I have a html table where i want to echo content out of a database table:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
</td>
<td>
<?php
$ergebnis = mysql_query("SELECT Test2 FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test2;
}
</td>
</tr>
</tbody>
</table>
Is there a way to cut short the php code, because it´s a bit too long to wirte in every
<?php
$ergebnis = mysql_query("SELECT Test FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->Test;
}
If your example really is what you are doing you might want to rather get all data in a single query:
<?php
$ergebnis = mysql_query("SELECT Test, Test2 FROM testtable");
$recordset = mysql_fetch_array($ergebnis, MYSQL_ASSOC);
?>
<table style="width: 100%;">
<tbody>
<tr>
<?php foreach (array('Test', 'Test2') as $column) { ?>
<td>
<?php foreach ($recordset as $record) { ?>
<?php echo htmlspecialchars($record[$column], ENT_QUOTES, 'UTF-8'); ?>
<?php } ?>
<td>
<?php } ?>
</tr>
</tbody>
</table>
Also note that the mysql_* functions has been deprecated and will be removed from the language soon. If you want to make sure your code doesn't start throwing notices or even will stop functioning in the future you might want to start using either mysqli_* or PDO.
http://php.net/manual/en/mysqlinfo.api.choosing.php
Also note that I have added htmlspecialchars() to prevent possible XSS attacks.
Well, you can simply do it by having a function:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
and calling it like this:
<table style="width: 100%;">
<tbody>
<tr>
<td>
<?php
$a = 'Test';
query($a);
?>
</td>
<td>
<?php
$a = 'Test2';
query($a);
?>
</td>
</tr>
</tbody>
</table>
Create a function out of it. And use wherever you want.
You can make a function like this:
function query($a){
$ergebnis = mysql_query("SELECT $a FROM testtable");
while($row = mysql_fetch_object($ergebnis))
{
echo $row->$a;
}
}
Related
How do I make a member list with PHP and MySQL?
I have user accounts, login and that stuff. How do I make a page that shows all the members?
I can provide code!
NOTE: Your question is not clear. You have not included any code or query snippet. I will try my best to answer your question.
As per my knowledge I think your users are nothing but the members. Just write a query to pull all the users from the users table and display.
NOTE : I will use mysqli_* function without any escaping please help yourself.
<?php
include_once 'db_connect.php'; /Line to include the database connection file, which had $link as resource */
$membersQuery = mysqli_query($link, "SELECT * FROM users");
$members = array();
if(mysqli_num_rows($membersQuery) > 0){
while($row = mysqli_fetch_assoc($membersQuery)){
$members[] = $row; //Get all the members row by row and store in $members array
}
}
?>
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?
if(count($members) > 0){
foreach($members as $member){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Even while looping in table you can use the following command
<tbody>
<?
if(mysqli_num_rows($membersQuery) > 0){
while($member = mysqli_fetch_assoc($membersQuery)){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
I have a problem with my first attempt at making a foreach loop.
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
It's being displayed like so on the page:
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK
Hope you can help me:)
<?php
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$counter=0;
while($row= mysql_fetch_assoc($result))
{
$assoc_query[$counter] = $row;
$counter++;
}
foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $value['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $value['name']; ?> </div>
</td>
</tr>
<?php } ?>
Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
//Do your thing
}
Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.
this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless
instead you need a while loop like this
<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
You should do it like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.
#Nerdlyist
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
Here's a screenshot of the page that I want to put a pagination Below is my code and I want to create a simple pagination. I tried some examples available in this site but unfortunately it doesn't work for me or I might have missed something in the code.
<?php
session_start();
$server = 'localhost';
$user = 'root';
$pass = 'root';
$connect = mysql_connect($server,$user,$pass)
or die(mysql_error());
$selectdb = mysql_select_db('des')
or die(mysql_error());
?>
<form method="post" name="action_form" action="admin2.php">
<div id="gallery1" class="lbGallery">
<table class="table" width="100%" cellpadding="10">
<tbody>
<?php
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit 4');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
</form>
Try this. What is does is:
create a contant $amount en a variable $offset
Create a link (next) with which sends the value of $offset back to the script
Catch the value in $_GET['offset'];
Add the value of $amount to $offset to create a new offset.
Run MySQL statement again with the new values for LIMIT
I didn't actualy test this code for typo's, but you'll get the general idea. Hope this is of any help.
(Best to use the new mysqli statement by te way).
<?php
$amount = 4;
if (!empty($_GET['offset']))
{
$offset = $_GET['offset'] + $amount;
}
else
{
$offset = 1;
}
$allRecords = mysql_query('select * from cms ORDER BY id DESC limit $amount,$offset');
if(is_resource($allRecords))
{
while($row = mysql_fetch_assoc($allRecords))
{
?>
<tr><ul>
<td width="30%"><li style="list-style:none"><a href="uploads/<?php echo $row['image'];?>"/><img src="uploads/<?php echo $row['image'];?>"/></li></td>
<td style="float:left; font-size:16px"><?php echo $row['name']; ?></td>
</ul>
</tr>
<tr>
<td colspan="2">
Next
</td>
</tr>
<?php
}
}
?>
how to display blank if there is no image in a record.As i have inserted a record in database without an image but while fetching an record it is displaying an blank image in front end.It should not show any image if there is no image.Here is my code. If there is no image it should show only description.
Blogimage.php
<tbody>
<?php include "blogs.php" ;
while($row = mysql_fetch_array($result))
{?>
<tr>
<td><img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/></td>
</tr>
<tr>
<td><?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?></td>
</tr>
<?php }?>
</tbody>
Blogs.php
$id=$_GET['title'];
$res = "SELECT * FROM blogs
WHERE blog_title='$id'";
$result=mysql_query($res);
try changing this
<img src="admin/upload/<?php echo $row['image'];?>" height="100" style="width:60%;height:50%;"/>
to this
<?php if($row['image']) echo "<img src='admin/upload/".$row['image']."' height='100' style='width:60%;height:50%;' />"; ?>
A couple of general points before we go into this
1) The MySql library you are using is old and busted - you should be using something newer - read this - the options are Mysql PDO and MySqli - I promise you, you will be glad you did, i use IDIORM as an interface and find it very good (Also helps prevent stuff in point 2 below!)
2) Your code can be SQL injected - more about that here - this is very bad :)
Below is an example, I have fixed the SQL injection problem, and put in some logic that would test to see if there is an image, if not, it will dump out a 'noimage.jpg' src value.
I have removed the include, and just simply put the code for blogs.php inline.
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$iamgeSrc = 'admin/upload/' . $row['image'];
}
else
{
$imageSrc = 'admin/upload/noimage.jpg';
}
?>
<tr>
<td>
<img src="<?php echo $imageSrc; ?>" height="100" style="width:60%;height:50%;"/>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
If you want no image to show at all, then this would work
<tbody>
<?php
//START CODE FOR BLOGS.PHP
$query = sprintf("SELECT * FROM blogs WHERE blog_title='%s'",
mysql_real_escape_string($_GET['title']));
$result = mysql_query($query);
//END CODE FOR BLOGS.PHP
while($row = mysql_fetch_array($result))
{
$image = '';
if(!empty($row['image']) && strlen($row['image']) > 4)
{
$image = '<img src="admin/upload/'.$row['image'].'" height="100" style="width:60%;height:50%;"/>';
}
?>
<tr>
<td>
<?php echo $image; ?>
</td>
</tr>
<tr>
<td>
<?php echo "<p style='width:60%;'>" .$row['blog_description']."</p>"; ?>
</td>
</tr>
<?php
}
?>
</tbody>
Here the value of $table will be passed from another file and it will be a table name in a database.
With that table name ($table) am trying to fetch its column names and all the values in the table and make the table look like an actual one we see in phpMyAdmin.
As a result of the code below. I can only get the column names. But not the data. Please tell me what should i do to perform the task of showing the datas in the table
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php
//echo"select * from $table";
$qq=mysql_query("show columns from $table");
if(mysql_num_rows($qq)>0){
//$i=1;
echo '<tr>';
while($rs = mysql_fetch_row($qq))
{
$sel=mysql_query("select * from $table");
$fetch=mysql_fetch_object($sel);
//while($fetch=mysql_fetch_object($sel))
//{
?>
<td><?php echo $fetch->$rs[0];?></td>
<?php
//}
//$i++;
}
echo '</tr>';
}
else
{
?>
<tr>
<td colspan="11">No data to display</td>
</tr>
<?php
}
?>
</tbody>
</table>
Yes only columns will be printing because you are printing $rs[0];?> where $rs holds object for mysql_query qq and it holds your column query only not
"select * from $table"
this.
Why dont you try displaying columns as a single row in html table and then try displaying data from other php set with seperate query structure .
Hope this way it helps.
I coudn't fix your code because it was getting ugly, so i remade it:
php:
function mysql_fetch_all($res) {
$result = array();
while ($row = mysql_fetch_row($res)) {
$result[] = $row;
}
return $result;
}
function getColumnsAndData($table) {
$table = mysql_real_escape_string($table);
$q1 = mysql_query("show columns from $table");
$q2 = mysql_query("select * from $table");
return array(mysql_fetch_all($q1), mysql_fetch_all($q2));
}
list($columns, $data) = getColumnsAndData($table);
?>
html
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="display" rel="datatable">
<thead>
<tr>
<?php foreach ($columns as $column): ?>
<td><?php echo $column[0] . ' ' . $column[1] ?></td>
<?php endforeach; ?>
<tr>
</thead>
<tbody>
<?php if (count($data) > 0): ?>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $value): ?>
<td><?php echo $value ?></td>
<?php endforeach; ?>
<tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td>No data to display</td>
</tr>
<?php endif; ?>
</tbody>
</table>