phatgreenbuds Posted October 10, 2008 Share Posted October 10, 2008 Not sure why this is not working...perhaps I am just too tired. For some reason the result keeps taking the else path and returning "Nothing" for the $path when I know for a fact there is an entry in the table. Basically I have a table with 5 entries and I want this loop to run 10 times. So during the first loop if it does not find row 1 (a slot field with the number 1 in it) it should come back with "Nothing" in the $path...then on the second loop it should find that row 2 (a slot field with the number 2 in it) does exist and should then place the "win_path" field contents into the $path....and so on $query = "SELECT * FROM $tblname WHERE tbl_hour = $hrcount"; $content = mysql_query($query); $num = mysql_num_rows($content); if ($num != 0) { $count = 1; $file = fopen("../test/testlist.wsx", "w"); $wsxpl = "<?wsx version=\"1.0\"?>\r\n"; $wsxpl .= "<smil repeatCount=\"indefinite\">\r\n"; while ($count <= 10) { while ($row = mysql_fetch_array($content)) { $exists[] = $row['slot']; if (in_array($count, $exists)) { $path = $row["win_path"]; } else { $path = "Nothing"; } } $wsxpl .="\t<media src=\"" . $path . "\"/>\r\n"; $count++; } $wsxpl .="</smil>"; fwrite($file, $wsxpl); fclose($file); } Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/ Share on other sites More sharing options...
sasa Posted October 10, 2008 Share Posted October 10, 2008 $wsxpl = "<?wsx version=\"1.0\"?>\r\n"; $wsxpl .= "<smil repeatCount=\"indefinite\">\r\n"; while ($count <= 120) { mysql_data_seek($content,0);// <-- insert this line while ($row = mysql_fetch_array($content)) { $exists[] = $row['slot']; Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662172 Share on other sites More sharing options...
phatgreenbuds Posted October 10, 2008 Author Share Posted October 10, 2008 Ok I am not gonna even pretend to understand why...but that fixed it. Guess I need to read up on what line does. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662176 Share on other sites More sharing options...
phatgreenbuds Posted October 10, 2008 Author Share Posted October 10, 2008 I spoke too soon. It did work partially but every row that is returned is the same. Its pulling the first row of the query instead of cycling through the array... Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662227 Share on other sites More sharing options...
phatgreenbuds Posted October 10, 2008 Author Share Posted October 10, 2008 Guys, I am sooo stuck on this...nothing I try seems to work...here is what I have code-wise. I stripped all the excess stuff out to simplify it. Loops 10 times. Current table has five rows. each loop should display the result of an existing row or display the word "nothing". The content of the existing 5 rows are different but all this returns is the content from the 5th row or nothing. $tblname = "test1"; $query = "SELECT * FROM $tblname WHERE tbl_hour = 1"; $content = mysql_query($query); $num = mysql_num_rows($content); if ($num != 0) { $count = 1; while ($count <= 10) { mysql_data_seek($content,0); while ($row = mysql_fetch_array($content)) { $exists[] = $row[tbl_slot]; if (in_array($count, $exists)) { $path = $row["win_path"]; } else { $path = "Nothing"; } } echo $count . ": " . $path; echo "<br />"; $count++; } } Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662303 Share on other sites More sharing options...
kenrbnsn Posted October 10, 2008 Share Posted October 10, 2008 Can you post a dump of your database and what you are trying to get as output? Ken Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662355 Share on other sites More sharing options...
phatgreenbuds Posted October 10, 2008 Author Share Posted October 10, 2008 unfortunately through some absurd circumstances I do not have the ability to dump the DB. Below is the code used to create the test DB I am allowed to run. $sql = "CREATE TABLE $tblname ( entry_id INT( NOT NULL AUTO_INCREMENT, win_path VARCHAR(50) NOT NULL, tbl_hour INT(3) NOT NULL, tbl_slot INT(3) NOT NULL, PRIMARY KEY (content_id) )"; Creates a pretty simple table. Each entry is common under "tbl_hour" which is equal to "1". "tbl_slot" is a unique numeric value between 1 and 10. This is used to designate the order the entries are to be played and writes them to the xml file. So to test this I put in only 5 even numbers in the DB with a unique value of 2, 4, 6, 8, 10, in the "tbl_slot" field...and in the "win_path" that has the different media that is to be written to the playlist in the order determined by "tbl_slot". The output I would expect is like this: 1: Nothing 2: image1 3: Nothing 4: image2 5: Nothing 6: image3 7: Nothing 8: image4 9: Nothing 10: image5 However with what I have now I am getting this: 1: Nothing 2: image5 3: Nothing 4: image5 5: Nothing 6: image5 7: Nothing 8: image5 9: Nothing 10: image5 I know the problem I just don't know or understand how to fix it. The query only finds five rows and I am asking it to basically fill in values for 10 rows then check each row to see if it exists and if it does then do something...if it doesn't do something else. Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662370 Share on other sites More sharing options...
phatgreenbuds Posted October 11, 2008 Author Share Posted October 11, 2008 ok here is a screenshot of the table...does this help? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662430 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2008 Share Posted October 11, 2008 This version gives the output you are looking at: <?php $query = "SELECT * FROM $tblname WHERE tbl_hour = 1"; $content = mysql_query($query); $num = mysql_num_rows($content); while ($row = mysql_fetch_assoc($content)) $exists[$row['tbl_slot']] = $row['win_path']; for ($count=1;$count<=10;$count++) { $path = (key_exists($count,$exists))?$exists[$count]:'Nothing'; echo $count . ': ' . $path . '<br>'; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662502 Share on other sites More sharing options...
phatgreenbuds Posted October 11, 2008 Author Share Posted October 11, 2008 YES! That works... I sorta follow what you did here but the line "$path = (key_exists($count,$exists))?$exists[$count]:'Nothing';" threw me off. How does that work? Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662507 Share on other sites More sharing options...
kenrbnsn Posted October 11, 2008 Share Posted October 11, 2008 It's short hand for <?php if (key_exists($count,$exists)) $path = $exists[$count]; else $path = 'Nothing'; ?> The "? :" is the Ternary Operator -- scroll down about 2 times. Ken Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662512 Share on other sites More sharing options...
phatgreenbuds Posted October 11, 2008 Author Share Posted October 11, 2008 I would be lying if I said I understood this...but now that it works I can start reverse engineering it and figure out the pieces. Thank you so very much, I was ready to give up. Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662513 Share on other sites More sharing options...
sasa Posted October 11, 2008 Share Posted October 11, 2008 this line move internal pointer of mysql object to start in 1st loop script goes to the end of qyery object and if you want use it again you mast move to its start Quote Link to comment https://forums.phpfreaks.com/topic/127905-solved-more-crazy-loops-in-the-nest/#findComment-662543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.