I have the following scenario. MySQL database and PHP page that get a set of user data from the database and displays them in a table. The code works fine and it is as follows:
<form id="iform" name="iform" method="post" action="<?php echo $_SERVER['SCRIPT_NAME'];?>">
<table class="tabcont" width="100%" valign="center" border="0" cellpadding="0" cellspacing="0" summary="main area">
<tr><td align="center" class="listlr" colspan="2">
</td></tr>
<tr><td align="center" class="listlr" colspan="2">
</td></tr>
<?php if (is_array($result) && count($result) > 0):
foreach ($result as $reskey => $resvalue) {
echo "<tr><td width='30%'>{$reskey}:</td>";
echo "<td width='70%'>";
if ($reskey == 'PortraitImage')
echo "<img id='{$reskey}' src='{$resvalue}' value=''/>";
else if ($reskey == 'SignatureImage')
echo "<img id='{$reskey}' src='{$resvalue}' value=''/>";
else
echo "<input class='formfld' id='{$reskey}' value='{$resvalue}'/>";
echo "</td></tr>";
}
else: ?>
<tr> <td>
<?php
echo "<div align=\"center\" class=\"listtopic\" id=\"iderror\">{$errormsg}</div>";
?>
</td>
</tr>
<?php endif; ?>
</table>
</</form>
What I need to do now is to display the data arranged in another way, in a html table like the one attached (which will have a background image but that's easyly implemented via css). The table needs to be in this format:
HTML Table
I am trying to modify the above code but can't seem to get it right in this table format.
I can certainly display it fine as it is in the above code or in a simple table, but it gets all messed up when I try to display the data in the above table format. Any suggestions based on the above code? Thanks.
Related
I want to create a PHP page to display the results of a query in a MySQL database under the format of a table. By spending quite some time on different forums, I ended with something that is somehow satisfying me but that is strongly affecting the design and the layout of my webpage. Due to the fact that I wrote the code by a test-fail strategy, it is far from being straightforward and I am sure it is possible to shorten and simplify it and, therefore, make it more compatible with the format of my webpage. Could anybody have a look at it and give some suggestions of general interest about how to solve this kind of issues?
<div id="main">
<?php
require_once('../mysqli_connect.php');
$response = $db->query("SELECT * FROM metabolite");
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">
<tr><td align="center"><b>Metabolites</b></td>
<td align="center"><b>KEGG Id</b></td>
<td align="center"><b>Synonyms</b></td></tr>';
while ($data = $response->fetch())
{
?>
<tr><td align="left">
<?php echo $data['Metabolite_name']; ?></td>
<td align="left">
KEGG: <?php echo $data['Synonyms']; ?></td>
<td align="left">
<?php echo $data['Synonyms']; ?></td>
</tr>
<?php
}
$response->closeCursor();
?>
</div>
I thank you in advance for all your effort and your help.
Tom.
There's no way we can improve the design and layout of your webpage with the code you've given us. What I can do is write 'better' readable code.
<?php
function tableCell($content)
{
echo '<td align="left">'.$content.'</td>';
}
// database access
require_once('../mysqli_connect.php');
// get all records from the metabolite table
$response = $db->query("SELECT * FROM metabolite");
// start main division
echo '<div id="main">';
// start the table
echo '<table align="center" cellspacing="2" cellpadding="5" border = "1">';
// walk through all the metabolite records
while ($data = $response->fetch())
{
// start a row
echo '<tr>';
// create the cells
tableCell($data['Metabolite_name']);
tableCell('KEGG: '.$data['Synonyms']);
tableCell($data['Synonyms']);
// finish a row
echo '</tr>';
}
// close the table
echo '</table>';
// close main division
echo '</div>';
// close query
$response->closeCursor();
But this is not worth much, the output should remain the same.
if ($response->num_rows > 0) {
while($data = $response->fetch_assoc()) {
echo "<tr><td>" . $data["Metabolite_name"]. "</td></tr>" . ;
}
}
else {
echo "0 results";
}
I need to pass value with href inside fetch array echo...
php code
$user = $get['username'];
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>GROUP NAME</b></td> <td><b>TASK TITLE 1</b></td> <td><b>TASK TITLE 2</b></td> <td><b>CREATED BY</b></td> <td><b>ASSIGNED TO</b></td> <td><b>DUE DATE</b></td> <td><b>PRIORITY</b></td> <td><b>CHANGE</b></td></tr> ";
while($result=mysqli_fetch_array($resource))
{
echo "<tr><td>".$result[0]."</td> <td>".$result[1]."</td> <td>".$result[2]."</td> <td>".$result[3]."</td> <td>".$result[4]."</td> <td>".$result[5]."</td> <td>".$result[6]."</td> <td> "<a href="changetask1.php?username='.$user.'">"</td></tr>";
} echo "</table></font>";
I need to pass username to next page with href value.
You are all messed up with quotes...
First... Learn to indent your code to make it readable.
Then, avoid echoing simple HTML if not necessary:
See your code after my «formatting»:
Try it, i've done a couple corrections...
<?php
$user=$get['username'];
$resource=mysqli_query($con,$sql);
?>
<!-- This is only HTML -->
<font color="#000000">
<h2 align="center"></h2>
<table align="center" border="1" width="50%">
<tr>
<td><b>GROUP NAME</b></td>
<td><b>TASK TITLE 1</b></td>
<td><b>TASK TITLE 2</b></td>
<td><b>CREATED BY</b></td>
<td><b>ASSIGNED TO</b></td>
<td><b>DUE DATE</b></td>
<td><b>PRIORITY</b></td>
<td><b>CHANGE</b></td>
</tr>
<?php
// This is a PHP block until the next ?>
while($result=mysqli_fetch_array($resource)){
echo "<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td>".$result[2]."</td>
<td>".$result[3]."</td>
<td>".$result[4]."</td>
<td>".$result[5]."</td>
<td>".$result[6]."</td>
<td><a href='changetask1.php?username=".$user."'></td>
</tr>";
}
?>
</table>
</font>
To avoid these problems of double and single quote you can write your code like following.
<td><a href="cheangetask1.php?username=<?php echo $user;?>"></td>
Now on the changetask1.php you can get username like this.
<?php echo $_REQUEST['username'];?>
When the user is browsing my system, the user would reach the page where he will choose a shipping option, which is currently using radio buttons. The list is from the database. What I need is:
when the user clicks an option, which is a picture by the way, not a standard radio button, another option will popup, to allow the user to choose a payment option, which is also in my database. I just need to make it pop up so the user will have two values to pass to the other page, namely for $carrier and $payment.
<?php
echo "<table><tr>";
$i = 0;
$qry="SELECT * FROM shipping";
$result= #mysql_query($qry);
while ($row=mysql_fetch_assoc($result)){
if ($i > 0 && $i % 3 == 0) {
echo "</tr><tr>";
}
echo "<td>";
?>
<table width="" cellpadding="3" cellspacing="0" border="1">
<tr class="row_submit">
<td height="250px" width="300px"><center><label>
<input type="radio" name="carrier" <?php if (isset($carrier) && $carrier=="row[ship_name]") echo "checked";?> value="<?php echo $row['ship_name']; ?>">
<!--<img src="../paymentoptions/lbc.png" alt="LBC" class="picture" width="245px" style="margin:10px"/></label></td> -->
<?php echo '<img src="data:image/jpeg;base64,'.base64_encode( $row['ship_pic'] ).'" height="210px" width="245px" style="margin:10px"/>'; ?>
</tr>
<tr class="row_submit">
<td height="180px" width="300px"><p><?php echo $row['ship_desc']; ?><p></td>
<tr>
<td>
<div id='price'> Additional ₱ <?php echo $row['ship_price']; ?></div></td></tr>
</table>
<?php
echo "</td>";
$i++;
}
echo "</tr></table>";
?>
I'm trying to use php with mysql. basically i've an index page where user fills a form and another page where all rows are displayed. i've checkboxes for each row for deleting the selected row/rows. i'm trying to create a new page (namely details) where it shows only the selected row.
I'm trying to use $_GET but i could not do it. maybe the syntax is wrong. any help is welcome.
here are the relative code parts:
display.php:
<?
require_once('auth.php');?>
<html>
<head>
<title>Goruntule</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<?
require "config.php"; // All database details will be included here
$page_name="display.php";
$start=$_GET['start']; // To take care global variable if OFF
if(!($start > 0)) { // This variable is set to zero for the first page
$start = 0;
}
$eu = ($start -0);
$limit = 10; // No of records to be shown per page.
$this1 = $eu + $limit;
$back = $eu - $limit;
$next = $eu + $limit;
// WE have to find out the number of records in our table. We will use this to break the pages
$query2=" SELECT * FROM table1 ";
$result2=mysql_query($query2);
echo mysql_error();
$nume=mysql_num_rows($result2);
/////// The variable nume above will store the total number of records in the table////
/////////// Now let us print the table headers ////////////////
$bgcolor="#f1f1f1";
echo "<TABLE width=80% align=center cellpadding=5 cellspacing=0> <tr>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>#</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>ID</font></td>";
echo "<td bgcolor='dfdfdf' > <font face='arial,verdana,helvetica' color='#000000' size='2'>Time</font></td>";
echo "</tr>";
////////////// Now let us start executing the query with variables $eu and $limit set at the top of the page///////////
$query=" SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit ";
$result=mysql_query($query);
echo mysql_error();
//////////////// Now we will display the returned records in side the rows of the table/////////
while($rows = mysql_fetch_array($result))
{
if($bgcolor=='#f1f1f1'){$bgcolor='#ffffff';}
else{$bgcolor='#f1f1f1';}
echo "<tr>";
echo "<td><input name='checkbox[]' type='checkbox' value='" . $rows[id] . "'></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[id]</font></td>";
echo "<td align=left bgcolor=$bgcolor id='title'> <font face='Verdana' size='1'>$rows[DateTime]</font></td>";
echo "<td>Details</td>";
//here is the problematic line i guess
echo "</tr>";
}
echo "</table>";
////////////////////////////// End of displaying the table with records ////////////////////////
///// Variables set for advance paging///////////
$p_limit=100; // This should be more than $limit and set to a value for whick links to be breaked
$p_f=$_GET['p_f']; // To take care global variable if OFF
if(!($p_f > 0)) { // This variable is set to zero for the first page
$p_f = 0;
}
$p_fwd=$p_f+$p_limit;
$p_back=$p_f-$p_limit;
//////////// End of variables for advance paging ///////////////
/////////////// Start the buttom links with Prev and next link with page numbers /////////////////
echo "<table align = 'center' width='50%'><tr><td align='left' width='20%'>";
if($p_f<>0){print "<a href='$page_name?start=$p_back&p_f=$p_back'><font face='Verdana' size='2'>PREV $p_limit</font></a>"; }
echo "</td><td align='left' width='10%'>";
//// if our variable $back is equal to 0 or more then only we will display the link to move back ////////
if($back >=0 and ($back >=$p_f)) {
print "<a href='$page_name?start=$back&p_f=$p_f'><font face='Verdana' size='2'>PREV</font></a>";
}
//////////////// Let us display the page links at center. We will not display the current page as a link ///////////
echo "</td><td align=center width='30%'>";
for($i=$p_f;$i < $nume and $i<($p_f+$p_limit);$i=$i+$limit){
if($i <> $eu){
$i2=$i+$p_f;
echo " <a href='$page_name?start=$i&p_f=$p_f'><font face='Verdana' size='2'>$i</font></a> ";
}
else { echo "<font face='Verdana' size='4' color=red>$i</font>";} /// Current page is not displayed as link and given font color red
}
echo "</td><td align='right' width='10%'>";
///////////// If we are not in the last page then Next link will be displayed. Here we check that /////
if($this1 < $nume and $this1 <($p_f+$p_limit)) {
print "<a href='$page_name?start=$next&p_f=$p_f'><font face='Verdana' size='2'>NEXT</font></a>";}
echo "</td><td align='right' width='20%'>";
if($p_fwd < $nume){
print "<a href='$page_name?start=$p_fwd&p_f=$p_fwd'><font face='Verdana' size='2'>NEXT $p_limit</font></a>";
}
echo "</td></tr></table>";
?>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<INPUT TYPE="BUTTON" VALUE="Previous" ONCLICK="window.location.href='http://......../util'">
</FORM></td>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $id => $value)
{$sql="DELETE FROM table1 WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
?>
details
<?
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
detail.php
<?php
require_once('auth.php');
$host="localhost";
$username="";
$password="";
$db_name="";
$tbl_name="table1";
mysql_connect("$host", "$username", "$password")or die("Cannot connect ". mysql_error());
mysql_select_db("$db_name")or die("Cannot select DB ". mysql_error());
$num=$_GET['var1'];
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_row($result) or die(mysql_error());
?>
<table border="0" align="center" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="13" align="center" bgcolor="#FFFFFF"><strong>Bölge</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Time</strong></td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['13']; ?></td>
<td bgcolor="#FFFFFF" align="center"><? echo $row['0']; ?></td>
</tr>
<tr>
<td colspan="14" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete">
<form>
<input type=button value="Close" onClick="javascript:window.close();">
</form>
</tr>
<?php
$checkbox=$_POST['checkbox'];
if($_REQUEST['delete']=='Delete'){
foreach($checkbox as $key=>$value)
{$sql="DELETE FROM $tbl_name WHERE id='$value'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=display.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>
<html><head><link href="loginmodule.css" rel="stylesheet" type="text/css" /></head></html>
As I've said, the only problem i'm guessing is with the syntax, or something small as i can echo the row when i gave the var1 a specific id.
I'm sorry if i'm reposting but i couldn't find an answer. Thanks!
Edit: I'm thinking of deleting checkbox parts and adding gif links in the while loop where users can delete, edit or detailed view of the corresponding row. seems easier i guess.
Make sure that the details link is within the while block.
<?php
while($rows = mysql_fetch_array($result)) {
echo 'Details';
}
?>
Looks like you should not be using double quotes.
<? echo "$rows[id]" ?>
Should be
<?php echo $rows[id]; ?>
I also suggest you use 'id' as the name of the get rather than 'var1'. 'var1' does not mean anything whereas 'id' makes more sense.
Details
It's because you are using $rows outside your while-loop. You have to put the
Details
inside the while loop.
//EDIT:
Alright, I've stripped your code to the very neccessary according to your problem. So don't just copy/paste the code, it probably won't work. But read it carefully, and I hope you get the idea and see what may be wrong with your code ;)
display.php
<?php
require_once('auth.php');
require "config.php";
$page_name="display.php";
$start = (isset($_GET['start']) && $_GET['start'] < 1) ? 0 : $_GET['start'];
$eu = ($start-0);
$limit = 10;
$query="SELECT * FROM table1 ORDER BY id DESC limit $eu, $limit";
$result=mysql_query($query);
echo mysql_error();
$i = 0; //counter for the bg-color
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="checkboxForm">
<table width="80%" align="center" cellpadding="5" cellspacing="0">
<?php while($rows = mysql_fetch_array($result)) :
$bgcolor = $i%2 == 0 ? '#ffffff' : '#f1f1f1';
?>
<tr>
<td>
<input name="checkbox[]" type="checkbox" value="<?php echo $rows['id']; ?>">
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="title">
<?php echo $rows['id']; ?>
</td>
<td style="align: left; font-family: Verdana; font-size: 10px; background-color: <?php echo $bgcolor; ?>;" id="date">
<?php echo $rows['DateTime']; ?>
</td>";
<td>
Details
</td>
</tr>
<?php endwhile; ?>
</table>
</form>
detail.php
<?php
require_once('auth.php');
$num= isset($_GET['var1']) ? $_GET['var1'] : '';
$query = "SELECT * FROM table1 where id='$num'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());
?>
<form name="form1" method="post" action="">
<table border="0" align="center" cellspacing="1" cellpadding="0">
<input name="checkbox[]" type="checkbox" value="<? echo $rows['id']; ?>">
<? echo $row['13']; ?>
<? echo $row['0']; ?>
<input name="delete" type="submit" id="delete" value="Delete">
<button onClick="javascript:window.close();">Close</button>
</table>
</form>
And not only for security's sake, you should get more familiar with PHP and some design patterns before publishing your website.
I have a webpage that does a MySQL query on a table. I have it to echo it out in a table and that works as it should, example:
$query = mysql_query("SELECT * FROM table");
while($row = mysql_fetch_array($query){
// my table and db stuff echos out here
}
Now using the same mysql query $query I am trying to echo it out again on the same page using the same query underneath but my problem is it does not seem to work.
Now you maybe thinking what I'm doing is odd, but the reason why is the first code above echoes data out in a table, but also checkboxes next to the stuff echoed out as it is for a form. All that works fine but it seems I cannot do another while loop above on the same query. The second one is exactly the same as above; only difference is it's not a form.
Can I only do a while() and mysql_fetch_assoc once on a single query?
UPDATE:
I'm sorry I still don't understand properly.
Here's my code; could anyone edit it for me?
(I could not put php tags in the code to split up the HTML from the PHP code. Sorry for any inconvenience).
$q = mysql_query("SELECT * FROM table");
<h1> Vote for your favourite extension </h1>
<form method="post" action="<?php echo basename(__file__); ?>">
<table>
<tbody>
<tr class="odd">
<td colspan="3" class="cellfeat" style="text-align: center;">Vote for your favourite extension</td>
</tr>
<?php
if(!$q){
// query failed etc
} else { // query ok so display form
while($row = mysql_fetch_array($q)){
echo '<tr class="odd">';
echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
echo '<td class="cellfeat"><input type="checkbox" name="'.$row['id'].'" value="'.$row['id'].'" /></td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
<input type="submit" class="submitcontact" value="Vote" />
</form>
<h1>Extension Statistics</h1>
<table>
<tbody>
<tr class="odd">
<td colspan="3" class="cellfeat" style="text-align: center;">Voting Statistics</td>
</tr>
while($row = mysql_fetch_array($q)){
echo '<tr class="odd">';
echo '<td class="cellfeat"><img src="images/statimages/extensions.gif" alt="Extension Vote Image" /></td>';
echo '<td class="cellfeat">'.$row['checkbox'].'</td>';
echo '<td class="cellfeat">'.$row['count'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
The only proper way as follows:
first, collect your data into array
$data = array();
while($row = mysql_fetch_array($query){
$data[] = $row;
}
then, call a template
and use your data as many times as you wish:
<table>
<? foreach ($data as $row): ?>
<tr>
<td><?=$row['id']?></td>
<td><?=$row['name']?></td>
<? endforeach ?>
</table?>
If I understand you need to use this function
http://php.net/manual/en/function.mysql-data-seek.php
to move your pointer