How to echo html and row from database - php

I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.

You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>

Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];

You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>

Related

Group list of array in a table [duplicate]

This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 11 months ago.
I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...
But it's showing all the content in single column, How I can fix it?
It shows like that right now:
[![enter image description here][1]][1]
PHP:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links">« <a class="active" href="#">1</a> 2 3 4 »</div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
echo "<td>" . $row['id'] . "</td>";
echo "<tr><td>" . $row['username'] . "</td></tr>";
echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
</tbody>
</table>
[1]: https://i.stack.imgur.com/jPTR8.png
change while() loop code like below:
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>"; //put <tr> opening code inside, not outside
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "</tr>"; //</tr> need to be added at last
}
Try this
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?> </td>
</tr>
<?php
}
?>

Change cell color based on MYSQL value

I want to change td color using if statement but somehow my code is not affecting all rows
this is my code :
require_once("../model/materiel.class.php" . "");
$mt=new materiel();
$data=$mt->afficher_tous1();
echo '<table id="customers2" class="table datatable table-striped">';
echo "<thead>
<tr>
<th>Qte disponible</th>
<th>Alert</th>
</tr>
</thead>";
echo "<tbody>";
foreach($data as $t){
echo "<tr>";
if ($t['qte_disponible_m'] == 0){
echo "<td bgcolor='red'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] > $t['alert_m']){
echo "<td bgcolor='green'>".$t['qte_disponible_m']."</td>";
}else if ($t['qte_disponible_m'] == $t['alert_m']){
echo "<td bgcolor='yellow'>".$t['qte_disponible_m']."</td>";
}
echo "<td>".$t['alert_m']."</td>";
echo "</tr>";
}
echo "</tbody>";
echo"</table>";
the problem i have see the screenshot below :
If statement is like jumping next row
Add a class with the CSS background-color property (with !important if needed) to the TD instead of bgcolor. The bgcolor gets overwritten by the table-striped class.

Use data from an SQL query, but don't display column in table

EDIT: If I use $colName instead of $colname, I'd have no issues. Stupid, simple fix.
I have a query pulling 6 columns from multiple tables. The final column is necessary to be used in a dynamic URL I'm generating, but I don't want that column to display on my table. How can I hide it/make it not show up? Is there a way to call the data, but not use it in my table?
Query ex:
SELECT a, b, c, d, e, exTable.f as CID
FROM table exTable
JOIN <other tables>
WHERE stuff happens
Table (you can see that my header row will hide fine, but the content won't):
<table>
<thead>
<tr>
<th class="header"><strong>A</strong></th>
<th class="header"><strong>B</strong></th>
<th class="header"><strong>C</strong></th>
<th class="header"><strong>D</strong></th>
<th class="header"><strong>E</strong></th>
<th class="header" style="display:none"><strong>F</strong></th> <!-- THIS WORKS -->
</tr>
</thead>
<tbody>
<?
foreach($tableData as $row)
{
echo "<tr>";
foreach($tableColNames as $colName)
{
if ($colname=='CID') {
echo "<td style='display:none'>" . $row[$colName] . "</td>"; <!-- THIS DOES NOT -->
}
elseif ($colName=='e') {
echo "<td><a href='http://my.url.here/".$row[CID]."_Document.pdf' target='_blank'>" . $row[$colName] . " </a></td>";
}
else {
echo "<td>" . $row[$colName] . "</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
You have a typo: $colname should be $colName.
if ($colname=='CID') {
Stupid, simple solution: use $colName instead of $colname.

get and display multiple checkbox values from database using php

i having two check boxes when i am selecting both check boxes i want to show the value related to both from database ,but now i able to print only one id of check boxes i want to show both data can anyone help hoe to show both a value ,Below is my code:
html
<input type="submit" name="Update" src="/image/exporttt.png" style="margin:0px;cursor:pointer">
<tbody>
<?php
$clientid=$_GET['clientid'];
if($clientid!=""){
$sql = mysql_query("SELECT * FROM clientnetworkpricehistory");
while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
{
echo '<tr class="alt">';
$alt = 0;
}
else
{
echo '<tr>';
$alt = 1;
}
echo '
<td id="CPH_GridView1_clientid" style="width:140px" class=" clientid '.$rows["net_id"].'">'.$rows["clientid"].'</td>
<td id="CPH_GridView1_country" style="width:160px" class=" country '.$rows['net_id'].'">'.$rows["country"].'</td>
<td id="CPH_GridView1_networkname" style="width:156px" class="networkname '.$rows["net_id"].'">'.$rows["networkname"].'</td>
<td id="CPH_GridView1_mccmnc" style="width:250px" class=" mcc'.$rows["net_id"].'">'.$rows["mcc"].'</td>
<td id="CPH_GridView1_mccmnc" style="width:250px" class=" mnc '.$rows["net_id"].'">'.$rows["mnc"].'</td>
<td id="CPH_GridView1_oldprice" style="width:320px" class=" oldprice '.$rows["net_id"].'">'.$rows["pricefrom"].'</td>
<td id="CPH_GridView1_newprice" style="width:129px" class=" newprice '.$rows["net_id"].'">'.$rows["priceto"].'</td>
<td id="CPH_GridView1_oldroute" style="width:143px" class="oldsupplierroute '.$rows["net_id"].'">'.$rows["routefrom"].'</td>
<td id="CPH_GridView1_newroute" style="width:143px" class=" newsupplierroute '.$rows["net_id"].'">'.$rows["routeto"].'</td>
<td id="CPH_GridView1_comments" style="width:143px" class=" comments '.$rows["net_id"].'">'.$rows["status"].'</td>
<td id="CPH_GridView1_from" style="width:143px" class=" fromdate '.$rows["net_id"].'">'.date('d.m.Y H:i', $rows["datetime"]).'</td>
<td style="width:65px" class=" '.$rows["net_id"].'"><input type="checkbox" name="chk1" value=" '.$rows["net_id"].'"/></td>
</tr>';
}
}
?>
</tbody>
jssearch.php
<?php
//connecting to db
$variable=$_POST['chk1'];
foreach ($variable as $variablename)
{
$sql_select="SELECT * from clientnetworkpricehistory where net_id=$variablename";
$queryRes = mysql_query($sql_select);
print"$sql_select";
}
echo "<table border='1'>
<tr>
<th>country</th>
<th>networkname </th>
<th>mcc</th>
<th>mnc</th>
<th>datetime </th>
</tr>";
while($row = mysql_fetch_array($queryRes))
{
echo "<tr>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['networkname'] . "</td>";
echo "<td>" . $row['mcc'] . "</td>";
echo "<td>" . $row['mnc'] . "</td>";
echo "<td>" . $row['datetime'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
my out put was(i am checked two checkboxes and i am trying to print two check boxes id it only showing one can any one help me how to do that)
56
Warning: Invalid argument supplied for foreach()
Use name="chk1[]" in your HTML so the checkboxes will be posted as an array.
Besides that:
The mysql_* functions are deprecated, consider using mysqli_* or PDO
You are vulnarable to SQL-injection on your where net_id=$variablename"
I agree with Peter but you also might wanna fix this little typo:
'.$rows['net_id'].'">'.$rows["country"].'
That's line 2 of that large echo you have with all the <td>s. The $rows['net_id'] should be double quotes.
But you knew that...:)

How to create a table in HTML using PHP

I have 5 pictures stored in a folder and their links stored on the database.
I want to put them in a table of three columns on each row.
<body>
<center>
<table border='1'>
<?php
$host="";
$username="";
$password="";
$db_name="fruits_db";
$tbl_name="fruits_tbl";
$connection=mysqli_connect("$host","$username","$password","$db_name");
if (mysqli_connect_errno())
{
echo "The application has failed to connect to the mysql database server: " .mysqli_connect_error();
}
$result = mysqli_query($connection, "SELECT * FROM fruits_tbl")or die("Error: " . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;
for($i=1; $i<=$rows ; $i++)
{
echo "<tr>";
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
("<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
mysqli_close($connection);
?>
</table>
</center>
</body>
</html>
The above code I created, contains two FOR loops. The script should count the number of rows in the table, and then divide by 3(the number of columns on each row in the HTML table).
I wonder where I'm going wrong wit this code.
With your while($row = mysqli_fetch_array($result)){} inside your 1st for loop it will run through all your rows, before the outside loop runs 2nd/3rd time.
Here is another way to do it -
$counter = 1;
// start 1st row
echo "<tr>";
while($row = mysqli_fetch_array($result)){
// if the 4th cell, end last row, and start new row
if ($counter%3==1){
echo "</tr><tr>";
}
echo
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>";
// increase the counter
$counter++;
}
// close the last row
echo "</tr>";
You're looping through all the results in the first table cell.
Try something like this instead:
for($i=1; $i<=$rows ; $i++) {
echo "<tr>";
for($j=1; $j<=3; $j++) {
$row = mysqli_fetch_array($result);
if ($row) {
echo(
"<td width='180px' height='200px'>"
."<div class = 'fruit_image'>"
."<img src='"
.$row['fruit_image']
."'/>"
."</div>"
."<div class = 'fruit_title'>"
.$row['fruit_name']
."</div>"
."</td>"
);
}
}
echo "</tr>";
}
If you just want to format the display with a new row after every 3 records, you could use the modulus operator:
$cntr = 0;
echo '<tr>';
while($row = mysqli_fetch_array($result)) {
$cntr++;
echo '
<td width="180px" height="200px">
<div class="fruit_image">
<img src="'.$row['fruit_image'].'" />
</div>
<div class="fruit_title">'.$row['fruit_name'].'</div>
</td>';
if ($cntr % 3 == 0 && $cntr != $num_rows)
echo '</tr><tr>';
}
echo '</tr>';
Keep in mind however that all the solutions presented so far may leave you with a last row with one or two td elements. You can fill this if you desire with empty <td> </td> columns.
print "<center><table border=1>
<tr>
<td>id</td>
<td>name</td>
<td>company</td>
<td>branch</td>
<td>job title</td>
<td>contact</td>
<td>email</td>
<td>mgs</td>
</tr>";
while($row=mysql_fetch_array($query))
{
print "<tr>";
for ($i=0;$i<=(count($row)/2);$i++)
{
print "<td>$row[$i]</td>";
} print"</tr>";
}
}
else{echo " <p>No Records Found</p>";}
<?php
function studentTable($name,$grade){
echo "<tr> <td>$name</td><td>$grade</td></tr>";
}
?>
<table style="width:100%" border="solid">
<tr>
<th>Name</th>
<th>Grade</th>
</tr>
<?php studentTable("Sarah", 90) ?>

Categories