In wordpress I have stored the data in database as serialize method. Now I have fetched the data in an array. So the code is like this
global $wpdb;
$results = wpdb->get_results("SELECT * FROM `table` where `id` = 3");
foreach($results as $result) {
echo '<pre>';
print_r($result);
echo '</pre>';
}
This one is getting me data like this
Array
(
[0] => stdClass Object
(
[id] => 1
[title] => a:3:{i:1;s:8:"test1";i:2;s:8:"test2";i:0;s:0:"test3";}
[page] => a:3:{i:1;s:3:"dsa";i:2;s:4:"dsds";i:0;s:0:"sdvdsvds";}
)
)
Now I want to count the values and want to show all the values in listing. Here you can see I have 3 values for title and page. So I took title as counter. So I made like this
foreach($results as $result) {
$count = count(unserialize($result->title)); // This gave 3
for( $i = 0; $i<$count; $i++ ) {
echo '<li></li>'; // This gave me 3 li's that I wanted
}
}
Now to get all the values for title and page I need again foreach and unserialize method. So can someone kindly tell me what is the nice and smart way to show the data for title and words here(inside li’s). Any suggestions and help will be really appreciable.
You appear to be 90% of the way there, you simply need to show the data.
I have removed your call to
count
, as if it’s only used to work out how may times to loop through then you won’t need it. You can use aforeach
loop to loop through all items in an array.I added a line break (
n
) after every</li>
, this is only to make the generated markup a little more readable.I have also added spaces after each opening parenthesis and before each closing, this is inline with the WordPress coding standard.