phpretard Posted August 3, 2009 Share Posted August 3, 2009 If there are no answers in the DB it should read "No Answers Assigned" Can anyone see why it would just show up blank? The rest works fine ... if there are answers while($all=mysql_fetch_assoc($all_answers)) { $answer[]=$all['answer']; foreach($answer as $checkbox) { if (empty($answer)){ $answer_list = "<input type=\"checkbox\" disabled /> <font size=\"2\">No Answer Asigned</font><br />"; }else{ $answer_list = "<input type=\"checkbox\" name=\"answer\" value=\"".$all['id']."\" /> <font size=\"2\">".$all['answer']."</font><br />"; } } echo $answer_list; } //For the record it is properly indented in the code. The forum didn't indent for some reason. Thank you for looking! Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/ Share on other sites More sharing options...
WolfRage Posted August 3, 2009 Share Posted August 3, 2009 Don't you need to check prior to the foreach? Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889774 Share on other sites More sharing options...
mattal999 Posted August 3, 2009 Share Posted August 3, 2009 while($all=mysql_fetch_assoc($all_answers)) { $answer[]=$all['answer']; if (empty($answer)) { $answer_list = "<input type=\"checkbox\" disabled /> <font size=\"2\">No Answer Asigned</font><br />"; } else { foreach($answer as $checkbox) { $answer_list = "<input type=\"checkbox\" name=\"answer\" value=\"".$all['id']."\" /> <font size=\"2\">".$all['answer']."</font><br />"; } } echo $answer_list; } //For the record it is properly indented in the code. The forum didn't indent for some reason. Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889775 Share on other sites More sharing options...
phpretard Posted August 3, 2009 Author Share Posted August 3, 2009 Thanks for the quick response but, even the new code gives me a blank... Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889783 Share on other sites More sharing options...
watsmyname Posted August 3, 2009 Share Posted August 3, 2009 Thanks for the quick response but, even the new code gives me a blank... trying changing if(empty($answer)) to if(count($answer)==0) Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889788 Share on other sites More sharing options...
GingerRobot Posted August 3, 2009 Share Posted August 3, 2009 By blank you mean a completely blank page? Do you have any other output that's supposed to be displayed? If so, chances are you have an error and have display errors turned off. Otherwise, is the while loop even running? Perhaps there are no rows in the result set. Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889794 Share on other sites More sharing options...
phpretard Posted August 3, 2009 Author Share Posted August 3, 2009 Here is the rest of the code: function get_all_answers(){ connect(); $question_id = mysql_query("select id from questions order by RAND() LIMIT 1") or die(mysql_error()); while($id_q = mysql_fetch_assoc($question_id)) { $db_id=$id_q['id']; } $the_question = mysql_query("select question from questions where id = '".$db_id."' ") or die(mysql_error()); while($single_q = mysql_fetch_assoc($the_question)) { echo "<b><font size=\"2\">".$single_q['question']."</font></b><hr />"; } $all_answers=mysql_query("select * from answers where question = '".$db_id."' order by RAND() LIMIT 4") or die(mysql_error()); while($all=mysql_fetch_assoc($all_answers)) { $answer[]=$all['answer']; if (empty($all['answer'])) { $answer_list = "<input type=\"checkbox\" disabled /> <font size=\"2\">No Answer Asigned</font><br />"; } else { foreach($answer as $checkbox) { $answer_list = "<input type=\"checkbox\" name=\"answer\" value=\"".$all['id']."\" /> <font size=\"2\">".$all['answer']."</font><br />"; } } echo $answer_list; } free($question_id); free($the_question); free($all_answers); } Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889796 Share on other sites More sharing options...
watsmyname Posted August 3, 2009 Share Posted August 3, 2009 the simple way to accomplish the thing will be like this <?php $answer=array(); $id=array(); while($all=mysql_fetch_assoc($all_answers)) { array_push($answer,$all['answer']); array_push($id,$all["id"]); } for($i=0;$i<count($answer);$i++) { if(!empty($answer[$i])) { $answer_list = "<input type=\"checkbox\" name=\"answer\" value=\"".$id[$i]."\" /> <font size=\"2\">".$answer[$i]."</font><br />"; }else{ $answer_list = "<input type=\"checkbox\" disabled /> <font size=\"2\">No Answer Asigned</font><br />"; } echo $answer_list; } ?> Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889800 Share on other sites More sharing options...
phpretard Posted August 3, 2009 Author Share Posted August 3, 2009 It still wonn't show "No Answer..." arrg Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889806 Share on other sites More sharing options...
watsmyname Posted August 3, 2009 Share Posted August 3, 2009 It still wonn't show "No Answer..." arrg hmmm try to debug it, try to echo query, copy this and run it in phpmyadmin and see if you get any results. Link to comment https://forums.phpfreaks.com/topic/168667-empty-array-check/#findComment-889811 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.