Jump to content

empty array check


phpretard

Recommended Posts

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

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

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

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

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

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.