Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop problem

Status
Not open for further replies.

ascikey

Programmer
Feb 18, 2004
127
GB
I am having problems getting the results from some checkboxes, the result only shows the first checkbox that has been ticked and ignores the others for some reason. I know that the correct values are there because I have printed then to the browser but when I am running through this loop to get the results from the database I only get one match to check.
Any ideas or criticisms are welcomed.
thx
here is the code
Code:
<?php
if(!empty($_POST['dvd']))
 {
  	include_once("connection.txt");
 	// select the database
	mysql_select_db($theDatabaseName,$conn) or die (mysql_error());
	$query = 'SELECT * FROM video';
	$querySet = mysql_query($query);
	$res = mysql_query($query, $conn);
	$numRows = mysql_num_rows($querySet);
	$numCols = mysql_num_fields($querySet);
	/////////////////////////////////////////////	
	// Get The table rows and print the customer order but each row will have to be checked
	// as many times as there are choices hmmm.. there is probably a simpler way but hey it works!:)
	$numResult=0;
	$numResult=count($_POST['dvd']);
	Print "You Have chosen $numResult DVDs";	

	foreach ($_POST['dvd'] as $value)
	{
		//go through the table/array, once for each choice
		for ($row = 0; $row < $numRows; $row++) 
		{	 		
    		$nextRow = mysql_fetch_row($querySet);
			//$temp = $nextRow[0];
			//print "this nextrow 0 $nextRow[0]<br>"; 
			if ($value == $nextRow[0]) 
			{	//this is the row in the database that the customer has
				//orderd so print to screen so they can check
    			for ($col = 0; $col < $numCols; $col++) 
    			{
        			echo  $nextRow[$col]; 
				}				
    		}
		}
	}

	////////////////
		
				
}
else
{
	print "<h1> No Selection made</h1>";
}
//////////////////////////////////////////////////////////////////////////////////////////////////
  ?>
 
Most important question:
Are you sure $_POST['dvd'] contains the correct data?
Code:
print_r($_POST);
This will tell.
 
Thanks for the replies, when i use
Code:
foreach ($_POST['dvd'] as $value)
{
   print "$value";
}
the correct result is returned
so I assumed that by using this as my outer loop i could then compare these values to the values in the database (where the data came from originally)

which loop is the problem? That is a good question one that I have been trying to answer all day, I have just had a thought if I put a different counter in all the loops I will found the one which is not performing correctly, I will do this and get back to u thx again.
 
ok i have found the bad line it is
Code:
$nextRow = mysql_fetch_row($querySet);

$nextRow only has a value on the first loop. does anybody know why this should loose its value. Do i have to reconnect to the database in the middle of a loop?
 
I see now what you are doing. I think you should change your approach. There is so much overhead in here...
Instead of loading the entire database into a record set and looping through it you should issue specific queries for each value in the DVD array.
Code:
foreach ($_POST['dvd'] as $value){
   # make a SQL query specific to the dvd
   $SQL = "SELECT * FROM video WHERE id= ".$value;
   # issue query
   $result = mysql_query($SQL) OR die("Lookup failed: ".$mysql_error());
   $row = mysql_fetch_assoc($result);
   # process the record now
   foreach($row as $key=>$val){
      echo("WHatever code here");
   }
}
This looks specifically for each checked item and makes a lookup in the database.
Your version looks through the entire database. It loops once and finds the right DVD. But then you would have to rewind and start in the beginning again, and again.
MySQL takes very little time to look and find a specific record. Imagine your way when you have let's say 100,000 DVDs.
Consider this solution.
 
ok thanks that looks a lot more streamline i will have a proper look at it and try to adopt this method
thx again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top