Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-25-2012, 08:41 AM   PM User | #1
j.trini
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
j.trini is an unknown quantity at this point
URGENT: mysql_fetch_array loop problems

Hi, I am a student working on a project that is a beginner at PHP. I am creating a search engine and have some problems. basically, I have a basic user search query and a premium user search query separated by an IF statement. My basic query works (minus the image display) while my premium query doesnt. It is supposed to access the db and run a mathematical calculation to get a weighting for each search result and then display the results in order. My code is getting stuck in the premium loop. Please help as this is due in some hours....Code:
Code:
<?php 

session_start();

//This is only displayed if they have submitted the form 
//if ($searching =="yes") 
//{ 
//echo "<h2>Results</h2><p>"; 

//If they did not enter a search term we give them an error 
//{
	
$searchbox = $_POST['searchbox'];
	
if ($searchbox == "") 
{ 
echo "<p>You forgot to enter a search term"; 
exit; 
} 

// Otherwise we connect to our Database 

$db_host = "localhost";
$db_username = "root";
$db_pass = "root";
$db_name = "qrate";

@mysql_connect ("$db_host", "$db_username", "$db_pass")  or die ("Could not connect to MySQL");
@mysql_select_db ("$db_name") or die ("No Database with that name");

// We preform a bit of filtering 

$searchbox = strtoupper($searchbox); 
$searchbox = strip_tags($searchbox); 
$searchbox = trim ($searchbox); 

//Now we search for our search term, in the field the user specified 


$data = mysql_query("SELECT * FROM ITEM WHERE item_desc LIKE '%$searchbox%'"); 

//And we display the results 
//For basic users...

$levelcheck = mysql_query("SELECT * FROM USER WHERE username = '".$_SESSION['username']."'");
$levelarray = mysql_fetch_array( $levelcheck );
if ($levelarray['USER_LEVEL'] == 'basic')
{
	while($result = mysql_fetch_array( $data )) 
	{ 
	echo $result['ITEM_NAME']; 
	echo "<br>";
	//$img = mysql_query("SELECT $result['ITEM_IMG']); 
	?><image src="<?php echo $result[ITEM_IMG]; ?>" /> 
	<?php
	echo "<br>"; 
	echo $result['ITEM_LINK']; 
	echo "<br>"; 
	echo "<br>"; 
	} 
}

//if premium user
else
{
	mysql_query("CREATE TEMPORARY TABLE priority (item VARCHAR(40), itemweight DECIMAL(2,1))");
	
	while($result = mysql_fetch_array( $data ))
	{
		$itemweight = 0;
		if ($result['ITEM_CAT1'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT1'];}
		if ($result['ITEM_CAT2'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT2'];}
		if ($result['ITEM_CAT3'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT3'];}
		if ($result['ITEM_CAT4'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT4'];}
		if ($result['ITEM_CAT5'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT5'];}
		if ($result['ITEM_CAT6'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT6'];}
		if ($result['ITEM_CAT7'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT7'];}
		if ($result['ITEM_CAT8'] == 1)
	 		{$itemweight = $itemweight + $result['USER_CAT8'];}
			
		mysql_query("INSERT INTO priority (item, itemweight) VALUES ($result[ITEM_NAME], $itemweight)");
	} 
	
	$weightqery = mysql_query("SELECT item, itemweight FROM priority");
	$weightarray = msql_fetch_array($weightqery);
	
	
	return $weightarray;
	
	//$prefmatch = mysql_query("SELECT $result['ITEM_NAME'], $result['ITEM_IMG'], $result['ITEM_LINK'] , $weightarray['itemweight'] FROM $result, $itemweight ORDER BY $weightarray['itemweight']  DESC");
	
	while($sortedresult = mysql_fetch_array( $prefmatch ))
	{ 
	 echo $sortedresult['ITEM_NAME'];
	 echo "<br>";
	 $img = $sortedresult['ITEM_IMG']; 
	 echo "<image src='img' />";
	 echo "<br>"; 
	 echo $sortedresult['ITEM_LINK'];
	}
	
}

//This counts the number or results - and if there wasn't any it gives them a little message explaining that 
$anymatches=mysql_num_rows($data); 
if ($anymatches == 0) 
{ 
echo "Sorry, but we can not find an entry to match your query<br><br>"; 
} 

//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$searchbox; 
//}
?>
j.trini is offline   Reply With Quote
Old 04-25-2012, 10:44 AM   PM User | #2
j.trini
New to the CF scene

 
Join Date: Apr 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
j.trini is an unknown quantity at this point
Progress

I have fixed the issue with the loop. I had changed the names of the attributes in the db but forgot to change them in the code. I am still having trouble in getting the temp table to work i think.
j.trini is offline   Reply With Quote
Old 04-25-2012, 10:48 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Always add proper error checks to your queries, like
Code:
mysql_query(...) or die(mysql_error());
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Tags
loop, mysql_fetch_array, php

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:43 AM.


Advertisement
Log in to turn off these ads.