Placing a MySQL variable inside <img src"xxxxx"> - php

This might sound a little vague, but I'm looking to place a variable from a MySQL database inside a line of HTML. Currently it looks like this:
.
Below is the code I am using.
Thanks!
echo "<table border='1'>";
echo "<tr> <th>Helo</th> <th>Class</th> <th>Need</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['Icon'];
echo "</td><td>";
echo $row['Class'];
echo "</td><td>";
echo $row['Need'];
echo "</td></tr>";
}
echo "</table>";
?>

Try:
echo '<img src="'.$row['Helo'].'" title="'.$row['Class'].'">';

Related

Looping rows in html table with php

I want to write the rows as many as the number stated in $row. (instead of one row only). How can I achieve this? What am I doing wrong? thanks.
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
echo "</table>";
echo $i+1;
}
?>
You are closing table inside the loop. Change to the following
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++)
{
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
}
echo "</table>";
?>
try this code i have added some mistake done by you
<?php
$row = 50;
echo "<table border='1'>";
for($i=0;$i<$row;$i++){ //add bracket here
echo "<tr>";
echo "<td>L1</td><td>L2</td><td>L3</td>";
echo "</tr>";
//echo $i+1; //remove this one
}
echo "</table>"; //close table tag outside the loop
?>

Output three colums from my mysql database php

I'm a beginner in php and i'd like to output 3 fields from my database to my website which included a bloc image,text and name. using a foreach i can only get 2 out of the three.
<table border="2px">
<tr>
<th> Brand</th>
<th> Images</th>
<th> Details</th>
</tr>
<?php
$tex = [];
$query = mysqli_query($conn, "SELECT * FROM upload");
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach (array_combine($det, $images) as $text => $image) {
echo "<tr><td>";
echo $name;
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $text;
echo "</td>";
echo "</td></tr>";
}
?>
</table>
that is a snippet of my code where images and text is outputted on the screen and receive and undefined variable in error in line 39 which would be echo $name;. what should i put in the foreach statement to get it to output all three?
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
can someone explain these to me as i have an idea what it is doing but i want to be 100.
This would work:
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach ($images as $idx => $image) {
echo "<tr><td>";
echo $br[$idx];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $det[$idx];
echo "</td>";
echo "</td></tr>";
}
You are combining only text and image. That way you would not know which name is the corresponding one. My loop solves this by using the index ($idx) for that. All arrays have the same size, so this works.
There's no point using a separate foreach loop like that. You should simply change your while loop like this:
while($row = mysqli_fetch_assoc($query)){
echo"<tr><td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'. base64_encode($row['image']) .'"/>';
echo"<td>";
echo $row['text'];
echo "</td>";
echo"</td></tr>";
}

Displaying records from database on webpage

I'm attempting to display my data in a table from my database, I used the exact same code for another table but for some reason the table appears but is empty.
Thanks its solved!
you have to put brackets in while loop, close tag and the last :
echo "<table border=1 cellpadding=5>";
echo "<tr>";
echo "<th>Workshop Name</th>";
echo "<th>Workshop Price (£)</th>";
echo "<th>Workshop Description</th>";
echo "<th>Further Information</th>";
echo "</tr>";
$SQL="select workshopId,workshopName, workshopPrice,workshopDescription from Workshops order by workshopId";
$exeSQL=mysql_query($SQL) or die (mysql_error());
while ($arrayworkshop=mysql_fetch_array($exeSQL))
{
echo "<tr>";
//display details
echo "<td>".$arrayworkshop['workshopName']."</td>";
echo "<td>".$arrayworkshop['workshopPrice']."</td>";
echo "<td>".$arrayworkshop['workshopDescription']."</td>";
echo "<td><a href='ViewWorkshop.php?workshopId=".$arrayworkshop['workshopId']."'>";
//Display name of the Task
echo "Further Information"."</a></td>";
echo "</tr>";
}
echo "</table>";

Table formatting in PHP

I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>
The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.
echo '<tr>'
and
echo '</tr>'
should be inside the wile loop

PHP: How to Insert an Image in a new row for every 10th row in the MySQL?

I would like to insert a new random image in a row (all by itself) after 10 rows of data have been populated.
I'm thinking I should input something like this:
if($counter % 10 == 0) {
echo 'image file';
}
But not really sure how to incorporate this in my code:
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td>";
echo "<td><H3>First Name</H3></td> <td><H3>Last Name</H3></td>";
echo "<td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count=$count+1;
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td> <H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter
$count=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter equals
$count++;
//insert an image every 10 rows
if($count==10){
$count=0;
echo 'yourimage.jpg';
}
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";
if($counter % 10 == 0) {
echo 'image file';
}
10, not 100 :). Put this after your $count=$count+1 statement in a td colspan=however many columns you have.
Thanks to the help of Here is the completed code that works (however, I still can't get the random image to show, but I can get an image :-)
echo "<table border='1' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>No.</H3></td><td><H3>Date</H3></td><td><H3>First Name</H3></td> <td><H3>Last Name</H3></td> <td><H3>City</H3></td><td><H3>Province</H3></td></tr>";
//declaring counter for data
$count=0;
//declaring counter for random image
$counter_for_image=0;
// keeps getting the next row until there are no more to get
while ($row = mysql_fetch_array( $data, MYSQL_ASSOC )) {
//counter for actual count
$count=$count+1;
//counter for image count
//so I can reset count and
//not affect actual count
$counter_for_image++;
/* does not work
// start random image code
$images = array(
0 => '1.jpg',
1 => '2.jpg',
);
$image = $images[ rand(0,(count($images)-1)) ];
$randomimage = "<img src=\"/http://wwww.my site.com/storm/images/".$image."\" alt=\"\" border=\"0\" />";
//print($output);
// end random image code
*/
// start every 10th row image display code
if($counter_for_image==10){
$counter_for_image=0;
echo '<tr><td colspan="6"><center><img src="http://www.mysite.com/storm/images/eric.jpg"/></center></td></tr>';
/* would use this if I could get random image to work
echo '<tr><td colspan="6"><center>';
print($randomimage);
echo '</center></td></tr>';
*/
} // end every 10th row image display code
// Print out the contents of each row into a table
echo "</td><td>";
echo $count;
echo "</td><td>";
echo $row['DateIn'];
echo "</td><td>";
echo $row['FirstName'];
echo "</td><td>";
echo $row['LastName'];
echo "</td><td>";
echo $row['City'];
echo "</td><td>";
echo $row['Province_State'];
echo "</td></tr>";
}
echo "</table>";

Categories