Echoing HTML in php
Hi I want to echo HTML in order to place variables from mysql into a HTML input tag.specially, a radio button I'm havin开发者_运维问答g problems, it says that it is expecting a comma or a semi colon. Here is my code:
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){
echo "<input type="radio";
echo "value=";
echo "$row["cnumber"]";
echo "/>";
echo "$row["cname"]";
echo "<br />";
Any idea why this isn't working?
Things like this won't work:
echo "<input type="radio";
you have to write it like this:
echo "<input type=\"radio\"";
"
is a special char, you have to place a \
in front of it if it is inside of a string.
Write this:
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){
echo "<input type=\"radio\" value=\"".$row['cnumber']."\" />".$row['cname']."<br />";
}
(not tested, but it should work?!).
echo '<input type="radio" value="'.$row["cnumber"].'" />'.$row["cname"].'<br />';
OR
echo "<input type='radio' value='{$row["cnumber"]}' />{$row["cname"]}<br />";
OR
echo "<input type=\"radio\" value=\"{$row["cnumber"]}\" />{$row["cname"]}<br />";
OR
echo '<input type=\'radio\' value=\''.$row["cnumber"].'\' />'.$row["cname"].'<br />';
OR
while($row = mysql_fetch_array($result)){
$html .= "<input type='radio' value='{$row["cnumber"]}' />{$row["cname"]}<br />";
}
echo $html;
OR
while($row = mysql_fetch_array($result)){
extract($row);
$html .= "<input type='radio' value='{$cnumber}' />{$cname}<br />";
}
echo $html;
OR
while($row = mysql_fetch_array($result)){
extract($row);
printf("<input type='radio' value='%s' />%s<br />", $row["cnumber"] , $row["cname"]);
}
- http://php.net/manual/en/language.types.string.php
You're quoting a quoted string:
echo "$row["cname"]";
Just do this:
echo $row["cname"];
you are very inconsistent with your quotes. The first line that breaks the script is echo "<input type="radio";
- should be: echo '<input type="radio"';
Don't echo HTML with PHP! (if you can avoid it).
It is more difficult to debug the HTML and more error prone (as you can see). You also loose any support from your IDE regarding HTML.
Instead, embed PHP into HTML:
<?php
//...
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result); // why are you doing this?
// this will skip the first row
?>
<?php while(($row = mysql_fetch_array($result))): ?>
<input type="radio" value="<?php echo $row['cnumber']; ?>" />
<?php echo $row['cname']; ?>
<br />
<?php endwhile; ?>
Reference: Alternative syntax for control structures.
Comments to your code: You have a great mess of quotation marks here:
echo "<input type="radio";
// ^ ^ ^
// └ start end ┘ └ start
echo "value=";
// ^ ^
// └ end └ start
// etc
Read more about strings. Make sure you always match the quotation marks correctly.
you can't use echo like that:
echo "$row["cname"]";
The double quotes in the array index are interpreted like the end of the string!
Hwr, if you're echoing only a variable you don't need the quotes!
echo $row["cname"];
will be enough : )
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){
echo "<input type='radio' ";
echo "value=";
echo $row["cnumber"];
echo "/>";
echo $row["cname"];
echo "<br />";
}
when there are quotes inside an echo, they must be different than the containing quotes
echo "<input type='hidden'>";
if the containing quotes are ", the quotes inside those quotes must be single quotes: '
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
while($row = mysql_fetch_array($result)){ ?>
<input name="test" type="radio" value="<?php echo $row['cnumber']; ?>" /><?php echo $row['cname']; ?><br />
<?php } ?>
Tip: If you want convert html to php, just copy your html code, after pasting in notepad. So, you call the command replace and change " to \".
精彩评论