phatgreenbuds Posted October 5, 2008 Share Posted October 5, 2008 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 More sharing options...
PFMaBiSmAd Posted October 5, 2008 Share Posted October 5, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657805 Share on other sites More sharing options...
Barand Posted October 5, 2008 Share Posted October 5, 2008 ...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 } } Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657809 Share on other sites More sharing options...
phatgreenbuds Posted October 5, 2008 Author Share Posted October 5, 2008 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>'; Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657810 Share on other sites More sharing options...
phatgreenbuds Posted October 5, 2008 Author Share Posted October 5, 2008 oooo that if(in_array" is a new one for me...looking that up now... Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657814 Share on other sites More sharing options...
Barand Posted October 5, 2008 Share Posted October 5, 2008 if a query finds no rows, it doesn't fail. The array will be empty so all slots get a dropdown Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657815 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 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? Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657826 Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657827 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 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? Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657860 Share on other sites More sharing options...
Bendude14 Posted October 6, 2008 Share Posted October 6, 2008 Wrong datatype for second argument is referring to the $unavailable variable. Its probably because your DB query did not return any results and therefore $unavailable is not an array... Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657866 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657875 Share on other sites More sharing options...
Bendude14 Posted October 6, 2008 Share Posted October 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657877 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 I put this in before the while loop and it seems to be working... if (mysql_num_rows == 0) { echo "Nothing here"; } else { //do the while loop Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657889 Share on other sites More sharing options...
Bendude14 Posted October 6, 2008 Share Posted October 6, 2008 Sorry i used mysql_affected_rows which was wrong mysql_num_rows was what i was thinking. Glad you fixed it anyway.. i should stay of the drink lol.... Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657891 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 crap...I spoke too soon. with what I had it saw every query returned as "0" so I tried changing it to null using the double quote like "" and now I am right back where I started. This is driving me to drink... :'( Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657894 Share on other sites More sharing options...
phatgreenbuds Posted October 6, 2008 Author Share Posted October 6, 2008 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" Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657902 Share on other sites More sharing options...
Barand Posted October 6, 2008 Share Posted October 6, 2008 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. Link to comment https://forums.phpfreaks.com/topic/127163-solved-some-complicated-theory/#findComment-657981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.