Click to See Complete Forum and Search --> : [RESOLVED] breaking array out into values?
thunder77
05-12-2006, 02:29 PM
I have some code that is running a search function and I cannot figure out how to effectively break the information out of the array of results for each listing... I have the code below and commented out where I'm having issus. this just prints the array values altogether. Since I need to make this look like a normal results page, I need to break this information out right? Is it something with $array['value'] ???
Any help is greatly appreciated as always!
<?php
$search = do_search($_POST['keywords'], $_POST['store'], $_POST['category'], $_POST['sub_category'], $_POST['price']);
$num_result = count($search);
if(is_array($search)){
echo "Your search returned ".$num_result." result(s).";
print_r($search); //how do I break this up into the information I need to show on each result?
}elseif(!$search){
echo "Please enter search terms";
}elseif($search == "NO_MATCH"){
echo "Your terms did not match any entries";
}
?>
thunder77
05-12-2006, 02:42 PM
Do I use a foreach or matrix array? I'm totally confusing myself by trying to find this online... please help me understand.
tabzter
05-12-2006, 02:46 PM
I dont know how the array that contains the search records is formatted...
if its in the style:
array[0] = array();
array[1] = array();
array[2] = array();
which is the most probable scenario... then loop through the array ....example;
$search = .....
foreach ($search as $item) {
echo $item[0];
echo $item[1];
echo $item[2];
}
although Ive used numbers in $item its probably best if you store your info in an associative array...like
$array['name'] = "Arnold Schwarzenneger";
$array['occupation'] = "Actor";
$array['age'] = "??";
thunder77
05-12-2006, 02:53 PM
man I wish that made sense to my lame brain
NogDog
05-12-2006, 02:55 PM
Can you show us the results of your print_r()? Then we'll have a better idea what the result looks like and how to deal with it.
thunder77
05-12-2006, 03:03 PM
Array
(
[0] => Array
(
[id] => 16
[unique_id] => dca56519cba2e0aaa3f37f1b2da82b43
[date_added] => 1147216686
[price] => 45
[description] => it;s a cat...
[title] => cat
[category] => photography
[sub_category] => people
[artist_name] => rich
[thumbnail] => dca56519cba2e0aaa3f37f1b2da82b43.jpg
[agent] => thunder
)
[1] => Array
(
[id] => 17
[unique_id] => f67993498960f39370894c234b5ec2e6
[date_added] => 1147217268
[price] => 456
[description] => another cat photo
[title] => another cat shot
[category] => art
[sub_category] => painting
[artist_name] => rich
[thumbnail] => f67993498960f39370894c234b5ec2e6.jpg
[agent] => thunder
)
)
NogDog
05-12-2006, 03:20 PM
One way, if you want a table output:
echo "<table>\n";
// header row:
echo "<tr>"
foreach(array_keys($search) as $val)
{
echo "<th>$val</th>";
}
echo "</tr>\n";
// data rows:
foreach($search as $row)
{
echo "<tr>";
foreach($row as $val)
{
echo "<td>$val</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
thunder77
05-13-2006, 02:02 PM
I can't figure this out still guys... I used your code nogdog and it returned a parse error... any idea what is up? where is $val defined? in the foreach? man why can't I have a brain on this one...
thunder77
05-13-2006, 02:07 PM
ok, sorry I got tabzters working, but how can I define some variables so I can place the results into a table and make them look nice?
NogDog
05-13-2006, 04:20 PM
I can't figure this out still guys... I used your code nogdog and it returned a parse error... any idea what is up?
Missing semi-colon at the end of this line:
echo "<tr>";
That's and easy one - you should get used to finding those. :)
where is $val defined? in the foreach?
Yes, see www.php.net/foreach
man why can't I have a brain on this one...
I don't know. :p
thunder77
05-13-2006, 05:22 PM
I ended up going a different direction and did this:
echo "<table>\n";
foreach ($search as $item) {
$title = $item['title'];
$category = $item['category'];
$thumbnail = $item['thumbnail'];
echo "<tr bgcolor=\"$row_color\"><td align=\"center\" valign=\"middle\"><a href=\"#\"><img border=\"0\" src=\"display_image.php?thumb=$thumbnail\" height=\"70\" weight=\"50\"></td>\n<td align=\"center\" valign=\"middle\"><a href=\"#\">$title</td><td align=\"center\" valign=\"middle\"><a href=\"#\">$category</td></tr>\n";
}
echo "</table>";
Is that ok? It seems to work fine except that my colored rows that work on another page don't work here... any idea why that is?
thunder77
05-13-2006, 05:29 PM
caught it myself again... lol thanks all and I've marked this resolved.
BTW - for anyone watching the thread, I wasn't getting my colors returned because I was looking to the category in $row and it was not reading from the db, it's reading from the array as $item so that's how it is now and it works...
thanks all