Below is my code:
while ($row = mysql_fetch_array($result)) {
echo "
<tr>
<td>{$row['TeacherForename'].$row['TeacherSurname']}</td>
<td>{$row['StudentForename'].$row['StudentSurname']}</td>
</tr>";
}
I want TeacherForname and TeacherSurname to concatenate with each other and StudentForename and StudentSurname and when I researched it says use the . syntax to concatenate but it doesn't work.
How are you suppose to do it?
while ($row = mysql_fetch_array($result)) {
echo "
<tr>
<td>{$row['TeacherForename']}{$row['TeacherSurname']}</td>
<td>{$row['StudentForename']}{$row['StudentSurname']}</td>
</tr>";
}
As long as you do that in the string context - . cannot be treated as php concatenation operator. So just place two variables together and that's it.
Related
I'm trying to place the output of this PHP SQL query into a database table, but it is outputting all of the row data into one column.
if(isset($_POST['submit'])) {
$name = htmlentities($_POST['name']);
$parts = explode(" ", $name);
$lastname = array_pop($parts);
$firstname = implode(" ", $parts);
$connection = mysql_connect("localhost", "user", "password");
mysql_select_db("shoretoshore", $connection);
$result = mysql_query("SELECT ship_no, shipment_id, arrival_date, origin, destination, lname, fname from shipment, captain WHERE captain.capt_id=shipment.capt_id AND captain.fname='$firstname' AND captain.lname='$lastname'", $connection);
echo '<table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
<tr bgcolor="#FFFFFF" style="font-weight:bold">
<th>Shipment No.</th>
<th>Shipment Id.</th>
<th>Arrival Date</th>
<th>Origin</th>
<th>Destination</th>
<th>Last Name</th>
<th>First Name</th>
</tr>';
while ($row = mysql_fetch_row($result)) {
foreach ($row as $value)
print "<tr><td>"."{$value}"."</td></tr>";
echo "<br>";
}
echo "</table>";
}
How do I output the results of the query into an HTML table?
You are putting the $value inside quotation marks, it will be treated as string.
Try:
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
You need to explain the issue you're having. But from what I can see off the bat, you're looping through all the values of the row and outputting them as rows itself, instead of as a cell within the table row. The curly brackets around value are unnecessary as well, since you are concatenating the strings, you can just do '<tr><td>'.$value.'</td></tr>' OR "<tr><td>$value</td></tr>". PHP will parse variables within a string if the string is double quoted. I would also refrain from adding <br> tags as direct children of a table. If you need spacing between table rows, use padding and margins.
try this
while ($row = mysql_fetch_row($result)) {
print "<tr><td>".$row[0]."</td></tr>";
echo "<br>";
}
The problem is that you're outputting a <tr> tag for every row and column. You need to move the <tr> outside the inner loop.
Technically, you don't need to concatenate "{$value}" with the other two strings, but you really should pass $value through htmlspecialchars() to avoid producing incorrect HTML if the value contains any < or & characters. Eg:
while ($row = mysql_fetch_row($result)) {
print '<tr>';
foreach ($row as $value) {
$v = htmlspecialchars ($value);
print "<td>$v</td>";
}
echo "</tr>\n";
}
Also, you're not supposed to have a <br> element between table rows, which is why I replaced it with a newline above. Personally, I would skip the closing </td> and </tr> tags since they are optional in HTML.
Note also, that the MySQL extension is deprecated and no longer maintained. You should be using MySQLi or PDO these days.
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value)
{
echo "<td>".$value."</td>";
}
echo "</tr>";
}
I made a small comment/logbook script which puts a Username, Comment and Date in my database.
I use a POST method to send this to my PHP script. Then I display everything in a table. But the 'Comment' goes "through" the tabledata if it doesn't contain a space. This is because the script doesn't know where to split it so it just prints it out whole.
Here is my code:
<?php
if(isset($_POST['HandleAction']))
{
if($_POST['User'] != '' && $_POST['Text'] != '')
{
$User = $_POST['User'];
$Text = $_POST['Text'];
mysql_query("INSERT INTO Comments VALUES('$User', '$Text', CURRENT_TIMESTAMP)");
}
else
{
echo "<script>alert('Vul alle velden in om.');</script>";
}
}
echo "Comments" . "<br>" . "<hr>";
echo "<table id='Comments'>";
$results = mysql_query("SELECT * FROM Comments");
while($row = mysql_fetch_array($results))
{
echo "<tr> <th width='720'>" . $row['Name'] . "</th> <th>" . $row['Date'] . "</th> </tr>";
echo "<tr> <td>" . $row['Text'] . "</td> </tr>";
}
echo "</table>";
?>
So long story short: my string can't split if it doesn't contain a string and makes my way too long.
Thank you in advance.
You need to apply the word wrap style to the td that is too long. Although this may distort the display because a long word may be split over multiple lines. You could restrict the amount of characters the user can type in, like twitter does or validate the input to have a maximum amount of characters per word.
Hope that helps
See more information here on word wrap
I'm pretty new to programming, especially PHP so my question probably is the wrong way to go about things. Essentially I have a range of numbers, generated using a conditional loop, which have to be input into an echo table.
$numbers=30;
while ($numbers<=40)
{
if($numbers%2==0) {echo " " . $numbers . "<br />";;}
else {;}
$numbers++;
}
echo " <table border='1'>
<tr>
<td>Rectangle 1 <br> $numbers </td>
</tr>
</table>";
There is other stuff in the table, and I don't want the entire table to loop, so basically I'm wondering how I would get the whole list of values, from my while loop, into the table.
Try this one For table formate data
$numbers=30;
echo " <table border='1'>";
while ($numbers<=40)
{
if($numbers%2==0) {
echo "<tr><td>Rectangle 1 <br> $numbers </td></tr>";
}
else {;}
$numbers++;
}
echo "</table>";
This may helpful to you!!!!
I've written a php code to display all the images. But there's is something wrong in the code and I can't fix it. It's kind of a syntax error but I've wasted hours over it and still mixing up the "quotes(')"..here's my php code:
while($row = mysql_fetch_array($display_query)){
print "<tr><td>".$row['itemid']."</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>".$row['description']."</td><td>";
print "₹".$row['cost']."</td></tr>";
}
"<tr><td>".$row['itemid']."</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>".$row['description']."</td><td>";
Should be
"<tr><td>".$row['itemid'] . '</td><td><img src="resources/wh/'.$row['itemid'].'.png"/></td><td>'.$row['description']."</td><td>";
But mix ' and " make your code a mess, you could use HEREDOC to make it more readable.
while($row = mysql_fetch_array($display_query)){
echo <<<EOF
<tr>
<td>{$row['itemid']}</td>
<td><img src="resources/wh/{$row['itemid']}.png"/></td>
<td>{$row['description']}</td>
<td>₹{$row['cost']}</td>
</tr>
EOF;
}
Your concatenation of string and quotation is not right. Try this -
while($row = mysql_fetch_array($display_query)){
print "<tr><td>" . $row['itemid']."</td><td><img src=" . 'resources/wh/' .$row['itemid']. ".png'/></td><td>".$row['description'] . "</td><td>";
print "₹".$row['cost']."</td></tr>";
}
The simplest solution is just make proper pencuation in your code.
The correct code is:
print "<tr><td>".$row['itemid']."</td><td><img src='resources/wh/".$row['itemid'].".png'/></td><td>".$row['description']."</td><td>";
from db we get column value of rating $number=$row->rating ;
if we want TO print image as rating value in a table row then
then
$number=$row->rating ;
$middle="";
$first="<td width='200' align='left'>";
for($x=1;$x<=$number;$x++) {
$middle=$middle.img($fullimage_properties);
}
if (strpos($number,'.')) {
$middle=$middle.img($halfimage_properties);
$x++;
}
while ($x<=5) {
$middle=$middle.img($blankimage_properties); ;
$x++;
}
echo $last=$first.$middle."</td>";
$sql1=mysql_query("SELECT * FROM Persons", $con);
echo "<table border="3">
<tr>
<th>Name</th>
<th>Age</th>
</tr>";
while($info=mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td>" . $info['fname'] . "</td>";
echo "<td>" . $info['age'] . "</td>";
echo "</tr>";
}
echo "</table>";
this code is a part of a code which is trying to retrieve data from the table"Persons",
there is some error in this part of the code..
You have double quotes in the quoted html. Try using single quotes in stead, i.e.
echo "<table border='3'> <--- here
<tr>
<th>Name</th>
<th>Age</th>
</tr>";
Your code looks ok, except for the unescaped double quotes:
It should be:
echo "<table border=\"3\"> ... ";
or
echo '<table border="3"> ... ';
Make sure it is enclosed in <?php and ?>.
Also make sure your db columns names of fname and age really exist....
Make sure you're getting what you think back from the DB, by using print_r($info) or var_dump($info).
Finally, your connection $con could be broken / not working. You can check that by using:
if ( ! $con ) {
die('Could not connect: ' . mysql_error());
}
$sql1 = mysql_query("SELECT * FROM Persons", $con);
...
Beside double quotes in second line.
mysql_fetch_array return array indexed by integer if you want asoc array indexed by fields use mysql_fetch_assoc
while ($info=mysql_fetch_assoc($sql1)) {
...
}