Jump to content

[SOLVED] some complicated theory


phatgreenbuds

Recommended Posts

I have a script that draws out a grid of 120 slots. In each slot it places a drop down with selectable info.

 

Lets say I have an empty table. I run the query and of course there is nothing there then the script above should run as normal and draw all 120 slots with the default dropdown.

 

However if there IS an entry in the table numbered as slot 3 for instance, then the script above should see that and instead of drawing slot 3 should come back as "unavailable" then continue drawing the rest of the grid with the default dropdown.

 

Now I know how to do the typical "while($row = mysql_fetch_array($blahblah)" then pull that info out of the array, and I know how to nest an "if" statement in there, but I cannot make sense of this logically to create the functionality described above. 

 

I am not asking for the actual code but rather the logical flow that should make this work.

 

Link to comment
https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/
Share on other sites

Rather than execute a query for each individual slot, query the table for all the values at once and read them into an array in the order they would need to be examined in to produce the output. Then the code that produces the output can just examine the first entry in the array and produce the necessary output. After you have processed a slot that matches an entry in the array, unset() that value in the array so that the next comparison will examine the next value.

...or

 

save the unavailable slots form the db in an array

 

while($row = mysql_fetch_array($blahblah)
{
  $unavailable[] = $row['slot'];
}

 

now draw the slots

for ($i=1; $i <= 120; $i++)
{
   if (in_array($i, $unavailable))
   {
       // unavailable
   }
   else
   {
       // dropdown
   }
}

theres where I am getting myself confused. I am thinking along those lines but what about when the table is empty? The query fails and goes no further. Lemme show you what I have so far and maybe you can see where I am going wrong.

 

$query = "SELECT * FROM $tblname";
$content = mysql_query($query); 

echo '<table>';
$counter = 0;
$counterx = 1;

//while ($row = mysql_fetch_array($content) {  //this is commented out since the empty table causes the query to fail															
while ($counterx <= 120) {

	if ($counter == 6) {
						echo "<tr>";
						$counter = 0;
						}
					echo "<td align=\"right\">$counterx:";
					list_content(); // this is just a function that returns the dropdown.
					echo "</td>";
					$counter++;
					$counterx++;
	}
	echo '</table>';

Barand...all I can say is "wow!" I have been struggling with this for days.  Not only did you solve it but you solved another big problem for me by accident. I have been using a test file called test3.php all this time. So everytime went thru the cycle of testing there is a spot higher up in the script that refers to the original test1.php.  Thus I never saw any changes no matter what I was doing and all this time I thought I was failing. the re-write you provided me helped in discovering that. Thank you sooooooooo much.

 

Just curious what is your day job?

Barand...all I can say is "wow!" I have been struggling with this for days.  Not only did you solve it but you solved another big problem for me by accident. I have been using a test file called test3.php all this time. So everytime went thru the cycle of testing there is a spot higher up in the script that refers to the original test1.php.  Thus I never saw any changes no matter what I was doing and all this time I thought I was failing. the re-write you provided me helped in discovering that. Thank you sooooooooo much.

 

Just curious what is your day job?

 

Master of the universe.

Below is what I have ended up with. It does work but only when the query runs with a single condition. In other words if I add the second condition in the query of "tbl_region = $region" then I get a funky error also pasted below.

 

$query = "SELECT * FROM $tblname WHERE tbl_hour = $hours AND tbl_region = $region";
$content = mysql_query($query); 
echo '<table>';

while($row = mysql_fetch_array($content)) {
	   $unavailable[] = $row['tbl_slot'];
	}

			$counter = 0;
			for ($i=1; $i <= 120; $i++) {

				if ($counter == 6) {

				echo "<tr>";
				$counter = 0;
				}
					echo "<td align=\"right\">$i: ";
					if (in_array($i, $unavailable))
					{
					echo "Reserved";
					echo "</td>";
					$counter++;

					} else {

					list_content();
					echo "</td>";
					$counter++;

			}
			}
			}
			echo '</table>';
			}			


 

This returns the following error.  Unless both conditions in the query exist in the table. If one does not I get the error.

 


Warning: in_array() [function.in-array]: Wrong datatype for second argument in /var/www/test/test5.php on line 127
  

 

Line 127 is the "if (in_array($i, $unavailable))" line and I don't understand why. This line is looking for only a single value so why is the second condition of the query having any effect on it?

 

so I need yet another nested condition that says if $unavaliable does not exist then do something...or does it go back further to the "while($row = mysql_fetch_array($content))"? I am thinking its the latter as the query does not find anything to return to $row

it doesn't matter where you handle it just as long as you do.

 

You could have an if statement that selects everything if nothing was selected

 

if(mysql_affected_rows == 0) { //query again and select everything }

 

 

or you could check $unavailable before if(in_array  and if it doesnt have any values echo "no results returned" etc

FIXED! WOOOOOOOOHOOOOOOOO!!!!!

 

Really stupid errors on my part...due to lack of sleep and such.

 

if (mysql_num_rows($content) == 0) {
                echo "Nothing here";
            } else { //do the while loop

 

I forgot to specifiy the actual query result being "$content"

before

while($row = mysql_fetch_array($content)) {
	   $unavailable[] = $row['tbl_slot'];
	}

 

add this line to declare the array (so it's defined even if no slots found).

 

$unavailable = array();

 

PS Don't believe what DW tells you, I'm just a professional developer.

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.