I am relatively new to php and have a feeling that I am going the long way round when displaying data from mysql.
I have a table a I want to show a few fields from my database.
How would I achieve this without having to echo every bit of the table???
Here is the code:
<?php
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");
while ($row = mysql_fetch_array($query1))
{
echo ' <table id="account_table" style="width:550px; border:none; ">
<tr>
<td width="155">Contact Name</td>';
echo '<td width="335">';
echo $row['firstname'] ;
echo ' ';
echo $row['lastname'];
echo '</td>
</tr>
<tr>
<td>Email Address</td>
<td>';
echo $row['email'];
echo ' </td>
</tr>
<tr>
<td>Username</td>
<td>' ;
echo $row['user'];
echo '</td>
</tr>
<tr>
<td>Country</td>
<td>';
echo $row['country'];
echo '</td>
</tr>
<tr>
<td>Time Zone</td>
<td>GMT+1</td>
</tr>
<tr>
<td>Activated</td>
<td>16 Dec 2009</td>
</tr>
</table>';
}
?>
I would suggest to look at some templating engine like Smarty, that would allow you to separate presentation from php code.
You can fetch all data into an array first and then iterate over that array. There is no need to echo all that HTML with PHP.
At the top of your file, you should do all the processing (i.e. getting, validating data) and in the remainder you just write plain HTML, only printing the values with PHP.
This already gives you a certain degree of separation. Others mention template engines (Smarty, etc.). I don't think that you really need that, because PHP itself is a template engine.
Just don't get tempted to do sophisticated stuff in your presentation ;)
Also the alternative syntax for control structures is very useful for using in combination with the presentation as it is imho much more readable.
I changed the table structure a bit, because you were not generation valid HTML (you create a lot tables with the same ID in your original code).
This just generates one table, with a row for each customer.
<?php
$customers = array();
$query1 = mysql_send("SELECT firstname, lastname, email, user, country FROM customers WHERE id='".$_COOKIE['custid']."'");
while ($row = mysql_fetch_array($query1)) {
$cusomters[] = $row;
}
?>
<table id="account_table" style="width:550px; border:none;">
<tr>
<th width="155">Contact Name</th>
<th>Email Address</th>
<th>Username</th>
<th>Country</th>
<th>Time Zone</th>
<th>Activated</th>
</tr>
<?php foreach($customers as $customer): ?>
<tr>
<td width="335">
<?php echo $row['firstname'] ?>
<?php echo $row['lastname'] ?>
</td>
<td><?php echo $row['email'] ?> </td>
<td><?php echo $row['user'] ?></td>
<td><?php echo $row['country'] ?></td>
<td>GMT+1</td>
<td>16 Dec 2009</td>
</tr>
<?php endforeach; ?>
</table>
The basics seem right, although you will need to move the <table> tag out of the while loop, now you are creating a table for every entry and my guess is that that´s not what you want.
You also need to prepare your output for output to the browser with something like htmlspecialchars($row[...]). That way you avoid potential problems if the output contains html tags (javascript, etc.).
First some advices, keep a window/tab open to the php manual. (ie, mysql_send() function doesn't exist).
Indent your code, and try to find a consistent convention to keep your code readable.
You're inserting your table markup into your while loop which is wrong. Your table should wrap your loop.
Using $_COOKIE as is to select from your database is really bad. Filter and sanitize your input before to use it in any query.
To avoid mixing data presentation and business logic, you can take a look to template engine.
Look at 19 Promising PHP Template Engines to find one, or take a look at the wikipedia list.
Related
Hello and thanks for coming.
So I have the Category table with "n" values on it, and I have the table Price with "n" duplicated idCategorys (foreign Key) in it.
What I want to do is when selecting a Category from a dropdown menu a Html Table gets populated with all the prices with the Category i've chosen.
So far I've managed to get a dropdown select menu populated with php as follows:
<select name="categorias" required id="categorias" >
<?php $render = mysqli_query($con,"SELECT idCategoria FROM neomixlt_desarrollo.Categorias");
while($row1 = mysqli_fetch_array($render)) {
echo "<option value='" . $row1['idCategoria'] . "'>" . $row1['idCategoria'] ."</option>";
}
?>
</select>
And a table populated as follows
<table width="957" border="2" cellspacing="0">
<tr>
<th width="98" style="text-align: center">Categoria</th>
<th width="99" style="text-align: center">Articulo</th>
<th width="348" style="text-align: center">Precio a Domicilio(bs.)</th>
<th width="348" style="text-align: center">Precio en Planta(bs.)</th>
<th width="40" style="text-align: center"></th>
</tr>
<?php $render = mysqli_query($con,"SELECT * FROM neomixlt_desarrollo.precios_articulo WHERE idCategoria=$link ORDER BY idCategoria DESC ") or die(mysql_error()); ?>
<?php while($row1 = mysqli_fetch_array($render)):; ?>
<tr>
<td style="text-align: center"><?php echo $row1['idCategoria']; ?></td
><td style="text-align: center"><?php echo $row1['idArticulo']; ?></td>
<td style="text-align: center"><?php echo $row1['precio_domi']; ?></td>
<td style="text-align: center"><?php echo $row1['precio_plant']; ?></td>
<td style="text-align: center"><?php echo "<a href=editar_precios.php?pid=$row1[idArticulo]&cid=$row1[idCategoria]&precio_domi=$row1[precio_domi]&precio_plant=$row1[precio_plant]>editar</a>" ?></td>
</tr>
<?php endwhile; ?>
</table>
But they are not linked and I have not much idea on how to do this. im restricted to use php and html only and all similar examples I've found are using javascript or ajax, any idea? thanks in advance!
w/o JavaScript, JQuery, Angular, etc. available, you'll need to submit every time.
So load the page up in its original state, the user will choose their category. Now w/o JS available to go run updates nicely for you, you'll need to submit a form back to the server, then on the server side, get the POST category, and build the desired HTML and send that to the browser to view. This will have to be done every time they change categories.
I need to display a large amount of html as text(a table) which contains PHP variables. Now I would like to know if there is a command that echos the HTML as text, but keeps the PHP.
I know I can change the > with >. I'm just wondering if there is a better way to do this.
<table style="width:400px">
<tr>
<td><b style="font-size:27px;">$_POST['name']</b></td>
<td rowspan="2"> IMG</td>
</tr>
<tr>
<td><b style="font-size:23px;font-weight:400">$_POST['function']</b></td>
</tr>
</table>
This is only a part of it of course.
TL;DR: I want to echo the above text(as text, not code) but the variables from PHP need to execute as code.
Pushing it through the htmlentities function when you output the data should work. The function will convert whatever string you put in into one that looks exactly the same when rendered in HTML, which seems to be what you want.
Use htmlspecialchars and add curly braces around PHP variables:
$string = "<table style=\"width:400px\">
<tr>
<td><b style=\"font-size:27px;\">{$_POST['name']}</b></td>
<td rowspan=\"2\"> IMG</td>
</tr>
<tr>
<td><b style=\"font-size:23px;font-weight:400\">{$_POST['function']}</b></td>
</tr>
</table>";
echo htmlspecialchars($string);
Im trying to echo a variable into an already existing DIV within a table.
EG:
<table>
<tr>
<td class="error"> </td>
</tr>
</table>
<?php
echo "<td class='error'>".$variable."</td>";
?>
Note: the error td tag is already style and positioned within a table.
The method above causes a lone td tag to be created and added to the top of
the page.
I realise it's probably best to echo php in HTML rather than vice versa but
I need echo HTML in this instance.
Is this possible ?
The simplest solution, if I am understanding correctly:
<table>
<tr>
<td class="error"><?php echo $variable; ?></td>
</tr>
</table>
You have to echo in-place.
what would be the proper implementation of displaying an empty/blank table or just the table header if query result is empty?
**note/conditions
no page redirection
no creation of two tables, one for query with empty result and one for query with results
or is there a much, much better way to do this?
here is a sample code:
<?php if(isset($result)){ ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $key => $data){?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php }?>
</table>
<?php } ?>
the problem here is that it still throws an error on foreach loop.
When the query result returns false return an empty array instead
I hope this help , and ready for more help if needed
About the exception:
You check if $results is set, but then you try to loop over $result (without a trailing s). I believe you have a typo, which might be a part of the problem. Fixing the typo and making sure that the result is not empty (it might be set, but still empty) will probably fix the exception being thrown.
About always showing the header:
To display the header no matter what, move the if-statement to just before the the loop.
FYI - About well formatted HTML-tables:
To declare a header on a table, you usually make use of the <thead> element - to separate it from the content of the table. An example of a well-formatted HTML-table:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>john#email.com</td>
</tr>
</tbody>
</table>
More on formatting tables
In your case, I would put the loop around the <tr> element within the <tbody>.
<?php if(isset($result) && !empty($result)): ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $data): ?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php endforeach ?>
</table>
<?php else: ?>
No results found.
<?php endif ?>
Note that I prefer to use the long-form of statements for PHP templates, as it greatly improves readability.
I made some images which i need, currently i have :
$output .= "
<tr>
<td align='center'><b>{$name}</b></td> </tr>
<tr>
<td align='center'><img src='$avatar'/></td> </tr>
<td align='center'><b>Points</b>: {$points}</td> </tr>
<tr>
<td align='center'><b>Status</b>:<b><font color='green'> {$misc['text_status']}</font></b></td> </tr>
<tr>
<td align='center'><a href='".$link."'><font color='red'><b>See more statistics</b></font></a><hr></td></tr>
";
with script i get this(ie image) http://img717.imageshack.us/img717/7885/thisdn.png .
Now i need to output something like this http://img703.imageshack.us/img703/171/thatq.png (ie image) without mysql. May i have some examples of yours?
move <tr> before your for loop and </tr> after you for loop
or better one, place individual tables with float:left for each member
You could try to wrap every profile information (photo, points etc) in a div and not use tables at all. Then float these div-s to the right or left. Note that every div must have the same class.