Output three colums from my mysql database php - 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>";
}

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
?>

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

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

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'].'">';

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>";

PHP loop - creating a table using a loop and giving it headers

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP

Categories