0

Lately i have created a custom template to search for data in a database, and also displaying it, by the use of checkboxes. So, a user can select multiple checkboxes to search data on and the query searches for the selected checkbox values.

This all works and the queried data is being returned in a variable called $tmp. Though, some of the queried data is serialized data (uploaded with (yes, also) checkboxes) but some data are just regular strings (data that is uploaded with radio buttons for example). So a queried row can contain both serialized as unserialized data.

To display each data of each column in a table, i used a if(count($tmp)) and a foreach for every piece of data in the column(s). Though, as mentioned before, i have some serialized data that is being displayed now and obviously i only need the string of that serialized data. I have read something about unserializeing data and try'd a couple of things (one of them is showed in the code below) but i cant seem to get it to work + i don't really have much knowledge about the serialize/unserialize code.

So the question is, how can i unserialize the data that is being queried, and should i make an if statement that only selects the serialized date and leaves the normal strings alone? (or perhaps this doesn't matter because the normal strings wont be unserialized anyway with the unserialize function?)

The query code is as follows:

$tmp = $wpdb->get_results("
        SELECT
            name_of_living_lab, location_of_living_lab, type_of_living_lab, theme_of_living_lab, stage_of_living_lab, living_lab_document           
        FROM           
            wp_participants_database            
        WHERE 
            location_of_living_lab REGEXP ('$criteria_location') AND theme_of_living_lab REGEXP ('$criteria_theme') 
        ORDER BY 
            name_of_living_lab ASC
    "); 

I used by the way the REGEXP to select the string in the serialized data.

The PHP code that displays the queried data:

if(count($tmp)>0){
    for($i=0;$i<count($tmp);$i++){
        echo "<tr>";  
            foreach($tmp[$i] as $key=>$value){


                echo "<td>" . $value . "</td>";                 
            }           
        echo "</tr>";
    }
}

The PHP code with which i try'd to unserialize the data:

if(count($tmp)>0){
    for($i=0;$i<count($tmp);$i++){
        echo "<tr>";  
            foreach($tmp[$i] as $key=>$value){

            $mydata = unserialize($value);

                echo "<td>" . $mydata . "</td>";                
            }           
        echo "</tr>";
    }
}

The above code is not working. This returns blank values for the normal strings in the database and the word 'Array' for the serialized array values.

*P.S. do i need to call the unserialize function in my custom template to use it? And how do i to this?

Thank you in advance!

  • 1
    You shouldn't be doing SQL queries on serialised data, only madness and pain lies down this path. If I were you I would have used a custom post type and used post meta to store your attributes rather than a custom table, letting you use WP_Query instead of writing raw SQL – Tom J Nowell Sep 4 '14 at 10:43
  • @Tom J Nowell to be honest, i have no clue how i should exactly do that as i am not so familiar with the standard Wordpress post type and meta data.. I try'd to figure it out a while ago but i sorta concluded that the meta and post of wordpress is linked to blogging posts and such and not to separate wp database table data. The thing is, i just want to store data with a form to a specific database table (for backend users) and retrieve that stored data, with the use of checkboxes, from the database and show it in a decent table or layout.. (on a separate page). – Nicolas Sep 4 '14 at 11:35
  • Luckily you're on a website were you can ask these questions =) Custom post types don't have to be for a title + content, they don't even need to have a public interface ( e.g. nav menu items ) – Tom J Nowell Sep 4 '14 at 11:38
  • @Tom J Nowell Fair enough:) Though, looking at my code, how would it look like then. Could you give a code example with which you retrieve the data out of the database and displaying it. NOTE: The storing of the database data (with the form) is being done with a plugin called 'participant database'. This plugin creates its own database table and you can create your own columns with it. Though, because the plugin didn't have the option to search by checkboxes (rather only with a text search box) i created my own custom template. – Nicolas Sep 4 '14 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.