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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.