I am trying to print the value from database. the database contains the image URL.
I am retrieving the image url through php and printing it.
I have written this code to do so.
code:
<?php
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT name,thumbnail,link FROM courses';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "<tr>";
echo "<td><a href='".$row['link']."' title='Click to see schedule'><img src='".$row['thumbnail']."'></a></td>";
echo "</tr>";
}
mysql_close($conn);
?>
This code is retriving image URL and displaying images on table. But I want 3 images per row, now it is printing only 1 image per row.
How can i do this?
You need to change your code to only open and close the row every third image. Try this:
$i = 0;
while($row = mysql_fetch_assoc($retval))
{
if($i == 0) {
echo "<tr>";
}
echo "<td><a href='".$row['link']."' title='Click to see schedule'><img src='".$row['thumbnail']."'></a></td>";
if(++$i == 3) {
echo "</tr>";
$i = 0;
}
}
// To close any open rows at the end
if($i % 3 > 0) {
echo "</tr>";
}
$cc = 0;
//initially open table row
echo "<tr>";
while($row = mysql_fetch_assoc($retval))
{
$cc++;
echo sprintf(
'<td><img src="%s"></td>',
$row['link'],
$row['thumbnail']
);
//close tablerow and open new one every 3 rows
if($cc % 3 == 0){
echo "</tr><tr>";
}
}
//close last table row
echo "</tr>";
this code prints data in 3 columns in a row. Please try
$cols=3; $col=0;
$vars = "<table>";
$vars .= "<tr>";
while($row = mysql_fetch_assoc($retval)){
$col++;
$vars .= "<td>".$row['link']."</td>";
if($col==$cols){
$col=0;
$vars .= "</tr><tr>";
}
}
$vars = "</tr>";
$vars .= "</table>";
Hope this helps.
Related
I am new to web development and starting learning about CRUD.
My question is that while I successfully show the table listing 3 product on 1 row, that on the second row the product no 4 are missing and skipping to the product no 5 and keep missing every last 3 row.
function getData(){
global $connect;
$query = "SELECT id, name, category, price, image, info FROM product_data";
$results = mysqli_query($connect, $query) or die(mysql_error());
echo "<table border = 0 >";
$x=1;
echo "<tr class='homepagetable'>";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if($x<=3){
$x = $x + 1;
extract($row);
echo "<td class='homepagetable'>";
echo "<a href=itemdetails.php?id=$id>";
echo '<img src=img/' . $image . ' style="max-width:220px;max-height:240px;width:220px;height:240px;"></img><br/>';
echo '<div class="truncate">'. $name .'</div><br/>';
echo "</a>";
echo 'Rp.'.$price .'<br/>';
echo "</td>";
} else {
$x=1;
echo "</tr><tr>";
}
}
echo "</table>";
}
Thanks in advance!
Your problem is that you have a wrong condition here:
if($x <=3)... else{...}
Change the if/else to this:
if($x <3){$x++;}
//...do something
if($x == 3){
$x = 1;
//Close and open tr
}
And you need to close the <img> tag, and the last <tr> tag outside of the loop
Perhaps you can write it like this:
function getData()
{
global $connect;
$query = 'SELECT id, name, category, price, image, info FROM product_data';
$result = mysqli_query($connect, $query) or die(mysql_error());
echo '<table border="0">';
$x=0;
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
if($x % 3 == 0)
{
// number of articles is a multiple of 3 - so close the row
if($x) echo '</tr>'; // but only if there was at least 1 article
echo '<tr class="homepagetable">';
}
$x ++;
echo '<td class="homepagetable"><a href="itemdetails.php?id='.$row['id'].'">
<img src="img/'.$row['image'].'" style="max-width:220px;max-height:240px;width:220px;height:240px;"><br/>
<div class="truncate">'.$row['name'].'</div><br/>
</a>Rp. '.$row['price'].'<br/>
</td>';
}
if($x) echo '</tr>'; // only close the row if there was at least 1 article
echo '</table>';
}
I want every different record of database to be display on each table rows, but I'm unable to retrieve different record for each row and column. Please give me suggestion where I can paste than while block so it will give different result for each row and column.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$company=$row['company'];
$model=$row['model'];
$available=$row['availability'];
$price=$row['price'];
}
echo "<table border='2'>";
$j = 0;
for($j = 0; $j<5; $j++)
{
echo "<tr>";
for($i = 0; $i<3; $i++)
{
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><img src='abc.jpg' height='250' width='250'/></td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<table border='2'>";
echo "<tr>";
echo "<td><b>Brand : </b></td>";
echo '<td>'.$company.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Model : </b></td>";
echo '<td>'.$model.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Availability : </b></td>";
echo '<td>'.$available.'</td>';
echo "</tr>";
echo "<tr>";
echo "<td><b>Price : </b></td>";
echo '<td>'.$price.'</td>';
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
?>
You need to move the table rows inside the fetch loop or store the row in an array. I have simplified your tables to make the example clearer:
$result = mysql_query($sql);
if (!$result) {
/* Error */
}
echo '<table>';
while ($row = mysql_fetch_array($result)) {
echo '<tr><td><img src="', htmlspecialchars ($row['image']), '">';
echo '<tr><td><table>';
echo ' <tr><th>Brand<td>', htmlspecialchars ($row['company']);
echo ' <tr><th>Model<td>', htmlspecialchars ($row['model']);
echo ' <tr><th>Availability<td>', htmlspecialchars ($row['availability']);
echo ' <tr><th>Price<td>', htmlspecialchars ($row['price']);
echo ' </table>';
}
echo "</table>\n";
Some notes about the code:
Test the return value of mysql_query(). The query might fail.
Escape your output using htmlspecialchars().
You should use <th> elements for your headings and style those, instead of using inline <b> elements.
I added output of $row['image'] which might not do what you want.
And do not use the deprecated mysql extension. Use PDO or mysqli instead.
In your while loop you always rewrite the same variables, after loop you have only last record saved.
In the loop, you have to save records into array.
In your code you have nested tables, but in the first one, there is only one row and one table cell which contains another table. I use just nested table.
<?php
...
$result = mysql_query($sql);
$data = array();
while($row = mysql_fetch_array($result)) {
$data[] = $row;
}
if (count($data) > 0) {
echo '<table>';
foreach ($data as $row) {
echo '<tr>';
echo '<td>Brand: ' . $row['company'];
echo '<td>Model: ' . $row['model'];
echo '<td>Availability: ' . $row['availability'];
echo '<td>Price: ' . $row['price'];
}
echo '</table>';
} else {
echo 'no records';
}
?>
Not sure I fully understand what you are trying to accomplish but try this.
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$rec_limit = 10;
$scriptname=$_SERVER['PHP_SELF'];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('online_shopping'); // include your code to connect to DB.
$tbl_name="mobile_db"; //your table name
$start = 0;
$limit = 5;
$sql = "SELECT id,company,model,price,availability,image FROM $tbl_name LIMIT $start, $limit";
$result = mysql_query($sql);
echo "<table border='2'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['company']."</td>";
echo "<td>".$row['model']."</td>";
echo "<td>".$row['availability']."</td>";
echo "<td>".$row['price']."</td>";
echo "</tr>";
}
echo "</table>";
?>
Hi I'm attempting to display data retrieved from a mysql table horizontally in an html table using php. The code below works well except for the fact that it leaves out the first record (starts at the second record) in my database. I'm sure it has something to do with the counter but I can't seem to figure out how to get it to stop doing this. If anyone can point out my error I'd really appreciate it!
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query)
or die(mysql_error());
$row = mysql_fetch_array($result);
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
$i = 0;
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
if ($i==0) {
echo "<tr>\n";
}
echo "\t<td align=\center\">$first_name</td>\n";
$i++;
if ($i == $items) {
echo "</tr>\n";
$i = 0;
}
}//end while loop
if ($i > 0) {
for (;$i < $items; $i++) {
echo "<td> </td>\n";
}
echo '</tr>';
}//end ($i>0) if
echo '</table>';
}else {
echo 'no records found';
}
try and remove the 1st
$row = mysql_fetch_array($result);
you are calling it twice, that's why it skips 1 row in your while loop
try this simpler.
$items = 5;
$query = "SELECT * FROM members ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
echo '<table border="1">';
while($row = mysql_fetch_array($result)){
$first_name = $row['first_name'];
echo "<tr>";
for ($i=0 ; $i <= $items ;$i++) {
echo "<td align='center'>".$first_name."</td>";
}
}//end while loop
echo "</tr>";
echo '</table>';
}else{ echo 'no records found'; }
I have run into this issue before. Try the do while loop instead. Example
do {
// code
} while($row = mysql_fetch_array($result)); //end while loop
$row = mysql_fetch_array($result);
1) remove this line of code from ur scripts
2) only use while loop code instead.
How do I display out all the information in a database (All tables and records) using PHP? I read displaying tables as HTML table but how do I do it for all the tables?
I tried the example here:
http://davidwalsh.name/html-mysql-php
But it shows the table names, how do I also display all the values?
Okay got it .. try this
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "username", "password", "DB");
$result = $mysqli->query("SHOW TABLES");
while ($row = $result->fetch_row()) {
$table = $row[0];
echo '<h3>', $table, '</h3>';
$result1 = $mysqli->query("SELECT * FROM `$table`");
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
$column = $mysqli->query("SHOW COLUMNS FROM `$table`");
echo '<tr>';
while ($row3 = $column->fetch_row()) {
echo '<th>' . $row3[0] . '</th>';
}
echo '</tr>';
while ($row2 = $result1->fetch_row()) {
echo '<tr>';
foreach ($row2 as $key => $value) {
echo '<td>', $value, '</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
$mysqli->close();
you can use the recursive function to display the tree Structure of your database.
Here is the a sample code from http://webcodingeasy.com/PHP/Simple-recursive-function-to-print-out-database-tree-structure
<?php
function print_menu($id = 0)
{
// get all records from database whose parent is $id
$result = $query->select_result("*", "table", "where parent = '".$id."'");
//check if there are any results
if($result != 0)
{
echo "<ul> n";
while($row = $query->fetch($result))
{
//print result and call function to check if it has children
echo "<li>".$row['name']."</li> n";
print_menu($row['ID']);
}
echo "</ul> n";
}
}
print_menu();
?>
If you want to pull all tables with the values in mysql, you can use the following code:
<?php
mysql_connect ("localhost", "DB_USER", "DB_PASSWORD"); //your mysql connection
mysql_select_db('DB_NAME') or die( "Unable to select database"); //your db name
$tables = mysql_query("SELECT table_name FROM information_schema.tables WHERE table_schema='DB_NAME'"); //pull tables from theh databsase
while ($table= mysql_fetch_row($tables)) {
$rsFields = mysql_query("SHOW COLUMNS FROM ".$table[0]);
while ($field = mysql_fetch_assoc($rsFields)) {
echo $table[0].".".$field["Field"].", "; //prints out all columns
}
echo $table[0];
$query = "SELECT * FROM ".$table[0]; //prints out tables name
$result = mysql_query($query);
PullValues($result); //call function to get all values
}
//function to pull all values from tables
function PullValues($result)
{
if($result == 0)
{
echo "<b>Error ".mysql_errno().": ".mysql_error()."</b>";
}
elseif (#mysql_num_rows($result) == 0)
{
echo("<b>Query completed. No results returned.</b><br>");
}
else
{
echo "<table border='1'>
<thead>
<tr><th>[Num]</th>";
for($i = 0;$i < mysql_num_fields($result);$i++)
{
echo "<th>" . $i . " - " . mysql_field_name($result, $i) . "</th>";
}
echo " </tr>
</thead>
<tbody>";
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
echo "<tr><td>[$i]</td>";
$row = mysql_fetch_row($result);
for($j = 0;$j < mysql_num_fields($result);$j++)
{
echo("<td>" . $row[$j] . "</td>");
}
echo "</tr>";
}
echo "</tbody>
</table>";
} //end else
}
?>
I am using this and works fine in mysql, for mysqli you need to tweak it a very little.
My code below displays 10 entries, but it displays it in 10 rows and 1 column. I would like for it to display 10 entries, but in 2 columns and 5 rows. Can anyone help? Thanks in advance.
< ?php
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10")
or die(mysql_error());
echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No')
{
continue;
}
else{
echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
echo '<tr>';
}
}
echo '</table>'
?>
<?php
mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10") or die(mysql_error());
$i = 1; //The following logic only works if $i starts at '1'.
$numofcols = 2; //Represents the number of columns you want in the table.
echo '<table>'; //Open table.
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No'){
continue;
}
else{
//If it's the beginning of a row...
if( $i % $numofcols == 1 ){
echo '<tr>'; //Open row
}
//Table Cell.
echo '<td>'; //Open Cell
echo 'ID Number: '.$row['id'];
echo '<br/> <img src="'.$row['Pic'].'"> <br/>';
echo '</td>'; //Close Cell
//If we have already placed enough cells, close this row.
if( $i % $numofcols == 0) {
echo '</tr>'; //Close Row.
}
//Now that we've made a table-cell, lets increment our counter.
$i = $i + 1;
}
}
//So we make sure to close our rows if there are any orphaned cells
if( ($i % $numofcols) > 0){
echo '</tr>';
}
echo '</table>' //Close Table
?>
Please note that the mod logic will only work if the starting value of $i is '1'.
This code will create a table with 2 columns.
Not tested, but basically you can call $row = mysql_fetch_array() in the loop as well to grab another row.
while($row = mysql_fetch_array( $result ))
{
echo "<tr>";
echo "<td>";
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
echo "</td>";
$row = mysql_fetch_array( $result );
if($row)
{
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
}
else
{
// Empty cell
echo "<td> </td>";
}
echo "</tr>";
}
Use modulo for echo "< tr>"
$result = mysql_query("SELECT * FROM Members LIMIT 0, 10")
or die(mysql_error());
$i = 0;
echo '<table>';
echo '<tr>';
while($row = mysql_fetch_array( $result )) {
if ($row['Approved']=='No')
{
continue;
}
else{
echo '<td>';
echo "ID Number: ".$row['id'];
echo "<br/>";
echo '<img src="'.$row['Pic'].'">';
echo "<br/>";
echo '<hr>';
if ($i % 5)
echo '</tr>';
$i++;
}
echo '</table>'