Jump to content

college project... need a tiny bit of help :)


denismur

Recommended Posts

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

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
}
?>

@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.

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>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.