Jump to content

quote problems


mishuk

Recommended Posts

Hi I am having trouble with this code. The script that creates a drop down box populated with values from a table works fine on it own however when I have put it into the quotes and wanting to format it in a table it doesn't work.  could I have some help with how to quote the query so that it will work.

Thanks

 

$message .= '
<TABLE BORDER="0" cellspacing="5">
              <TR>
        <TD>Ethnicity:</TD>
        <TD>
	echo <select name="ethnicity">;
	$res=mysql_query("select * from tbl_ethnicity");

	if(mysql_num_rows($res)==0)
	echo "there is no data in table..";
	else
	echo"<option>Select:</option>";
		for($i=0;$i<mysql_num_rows($res);$i++)
		{
		$row=mysql_fetch_assoc($res);
		echo"<option>$row[ethnicity]</option>";
		}
		echo </select>;
</TD>
  	</TR>
</TABLE>
</FORM>';


Link to comment
Share on other sites

Thanks for your advice.  As you can probably guess my experience with php has been very little. 

So if i was to take it in its simplest context with the code below.  how would you suggest I tidy it up?

Thanks


<?php
include("includes/db_connect.php");

echo '<select name="ethnicity">';
	$res=mysql_query("select * from tbl_ethnicity");

	if(mysql_num_rows($res)==0)
	echo "there is no data in table..";
	else
	echo '<option>Select:</option>';
		for($i=0;$i<mysql_num_rows($res);$i++)
		{
		$row=mysql_fetch_assoc($res);
		echo "<option>$row[ethnicity]</option>";
		}
	echo '</select>';
?>

Link to comment
Share on other sites

<?php
include("includes/db_connect.php");

$res=mysql_query("select * from tbl_ethnicity");

if(mysql_num_rows($res)==0)
{
echo "there is no data in table..";
}
else
{
echo '<select name="ethnicity">';
echo '<option>Select:</option>';
while($row=mysql_fetch_assoc($res))
{
	echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>';
}
echo '</select>';
}
?>

 

That is how I would do it. Well I would break out of php for some of the html but otherwise thats how I'd do it.

Link to comment
Share on other sites

Ah right.  Yeh that does seem better.  however i am still left with my origional problem in that i am what i need to be able to put this code into a variable and call this variable into the html

 

<?php

$message = ' script ';

?>

<HTML>
<HEAD>
</HEAD>
<BODY>
<? 
print $message; 
?>
</BODY>
</HTML>

Link to comment
Share on other sites

Whatever i try as far as getting the quotes correct so that it works.  none of it does ?

<?php
include("includes/db_connect.php");

$message = '$res=mysql_query("select * from tbl_ethnicity");
if(mysql_num_rows($res)==0)
	{
	echo "there is no data in table..";
	}
	else
	{
	echo '<select name="ethnicity">';
	echo '<option>Select:</option>';
while($row=mysql_fetch_assoc($res))
	{
	echo '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>';
	}
	echo '</select>';
           }';

?>

<HTML>
<HEAD>
</HEAD>
<BODY>
<? print $message; ?>

</BODY>
</HTML>

Link to comment
Share on other sites

I am trying to have an input form that uses self submision so that i dont need to seperate my code from my html etc.  I want it to work so that it will only post the form when certain conditions are true else post something else.  Does that make sence ?

Link to comment
Share on other sites

Try that, ish.

 

<?php
include("includes/db_connect.php");

$res=mysql_query("select * from tbl_ethnicity");

if (mysql_num_rows($res)==0)
     {
     echo "there is no data in table..";
     }
else
     {?>
     <select name="ethnicity">
     <option>Select:</option>
     <?php
     while($row=mysql_fetch_assoc($res))
          {?>
  <option value="<?php echo $row['ethnicity_id']; ?>"> <?php echo $row['ethnicity']; ?> </option>
          <?php } ?>
     </select>
     <?php } ?>

<HTML>
<HEAD>
</HEAD>
<BODY>
<? print $message; ?>

</BODY>
</HTML>

Link to comment
Share on other sites

Jesus H.... If you want something doing properly.....

 

It is easier to perform this task as jessie suggest and just have it in a code block that sits inside the html, BUT if you want to put that bit of un-necessary extra load on the server memeory then do this...

 

include("includes/db_connect.php");

$res=mysql_query("select * from tbl_ethnicity");

if(mysql_num_rows($res)==0)
{
$mssg = "there is no data in table..";
}
else
{
$mssg = '<select name="ethnicity">';
$mssg .= "\r\n\t" . '<option>Select:</option>';
while($row=mysql_fetch_assoc($res))
{
	$mssg .= "\r\n\t" . '<option value="' . $row['ethnicity_id'] . '">' . $row['ethnicity'] . '</option>';
}
echo "\r\n" . '</select>';
}
?>

<HTML>
<HEAD>
</HEAD>
<BODY>

<?php echo $message; ?>

</BODY>
</HTML>

 

Now don't you worry about all those \r\n and \t's - they will just make it easier to read your html.

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.