denismur Posted April 23, 2008 Share Posted April 23, 2008 hi i'm college student trying to get my search page working, i hope someone can point me in the right direction.. i have two tables in mysql one called 'media' the other called 'keywords', so when you search it go though keywords table and bring back the results... the problems php... i just can't get it working... i tryed everthing (i know) any feedback welcomed thank in advanced <?php $fsearch = $_POST['fsearch']; if ($fsearch == '') { echo("You did not enter text in search box <br>\n"); exit; } if ($_POST['SEARCH']== "SEARCH") { $display_block .= " <table celpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"98%\"> <tr> <th>Media No</th> <th>Keyword No</th> <th>Title</th> <th>Keyword</th> </tr>"; $strToken=strtok($fsearch," "); $mysqli = mysqli_connect("localhost", "root", "root", "mediabox"); $query = "select media.media_no, keywords.keyword_no, media.media_title, keywords.keyword from media LEFT JOIN keywords on media.media_no = keywords.media_no where keywords.keyword like '%$strToken%' GROUP BY media.media_no"; $get_cart_res = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli)); while($strToken){ while ($cart_info = mysqli_fetch_array($get_cart_res)) { $mediano = $cart_info['media_no']; $keywordno = $cart_info['keyword_no']; $mediatitle = $cart_info['media_title']; $keyword = $cart_info['keyword']; $display_block .= " <tr> <td align=\"center\">$mediano <br></td> <td align=\"center\">$keywordno <br></td> <td align=\"center\">$mediatitle <br></td> <td align=\"center\">$keyword <br></td> </tr>"; } $strToken = strtok(" "); $display_block .= "</table>"; } } ?> <html> <head> <title>Search</title> </head> <body> <?php echo $display_block; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/ Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 Does it display anything? At all? Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524915 Share on other sites More sharing options...
denismur Posted April 23, 2008 Author Share Posted April 23, 2008 it dose show the first result, but that it... so say i search 'car', and i have 3 items with the keyword car, it would only show the first item not the rest... i think there something wrong with my WHILE loop Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524921 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 Change: while ($strToken) { To: while ($strToken !== false) { And see what happens. I'll go through it line by line with you and look for errors. Tell me if this outputs the right things. Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524924 Share on other sites More sharing options...
cooldude832 Posted April 23, 2008 Share Posted April 23, 2008 surprised yuo in a college course and they didn't teach you a mysql loop for a select should look like <?php $q = "Select * from` table`"; $r = mysql_query($q) or die(mysql_error()."<br /><br />".$q); if(mysql_num_rows($r) >0){ while($row = mysql_fetch_assoc($r)){ #Output table data here } } else{ #no rows } ?> Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524926 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 @cooldude832: May I direct your attention to: while ($cart_info = mysqli_fetch_array($get_cart_res)) { He's using MySQL Improved functions and he has it right. @Thread starter: You could do: while ($cart_info = mysqli_fetch_assoc($get_cart_res)) { That's what I do. I'm not sure what the default retrieval method is for mysqli_fetch_array. Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524929 Share on other sites More sharing options...
cooldude832 Posted April 23, 2008 Share Posted April 23, 2008 didn't see that saw that while $strtoken Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-524936 Share on other sites More sharing options...
denismur Posted April 23, 2008 Author Share Posted April 23, 2008 finally got it working, here the code if anyone interested <?php $medianocheck = array("teststring1","teststring2"); function insert_in_array_pos($array, $pos, $value) { $result = array_merge(array_slice($array, 0 , $pos), array($value), array_slice($array, $pos)); return $result; } $fsearch = $_POST['fsearch']; if ($fsearch == '') { echo("You did not enter text in search box <br>\n"); exit; } if ($_POST['SEARCH']== "SEARCH") { $display_block .= " <table celpadding=\"0\" cellspacing=\"0\" border=\"0\" width=\"98%\"> <tr> <th>Media No</th> <th>Keyword No</th> <th>Title</th> <th>Keyword</th> </tr>"; $strToken=strtok($fsearch," "); $mysqli = mysqli_connect("localhost", "root", "root", "mediabox"); while($strToken){ $query = "select media.media_no, keywords.keyword_no, media.media_title, keywords.keyword from media LEFT JOIN keywords on media.media_no = keywords.media_no where keywords.keyword like '%$strToken%' GROUP BY media.media_no"; $get_cart_res = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli)); while ($cart_info = mysqli_fetch_array($get_cart_res)) { $mediano = $cart_info['media_no']; $keywordno = $cart_info['keyword_no']; $mediatitle = $cart_info['media_title']; $keyword = $cart_info['keyword']; if (array_search("".$mediano."", $medianocheck)) { #bla bla } else { $display_block .= " <tr> <td align=\"center\">$mediano <br></td> <td align=\"center\">$keywordno <br></td> <td align=\"center\">$mediatitle <br></td> <td align=\"center\">$keyword <br></td> </tr>"; array_push($medianocheck, "".$mediano.""); } } $strToken = strtok(" "); } $display_block .= "</table>"; } ?> <html> <head> <title>Search</title> </head> <body> <?php echo $display_block; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-525222 Share on other sites More sharing options...
denismur Posted April 23, 2008 Author Share Posted April 23, 2008 SOLVED thanks for the help DarkWater Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-525360 Share on other sites More sharing options...
DarkWater Posted April 23, 2008 Share Posted April 23, 2008 No problem. Link to comment https://forums.phpfreaks.com/topic/102526-college-project-need-a-tiny-bit-of-help/#findComment-525363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.