Display selected values in mysqli columns [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
All i want to do is to output only Agric Department inside the Department columns. How do i go about this. Thanks in advance. This is my code.
<thead>
<tr>
<th>SL</th>
<th>Student Name</th>
<th>Department</th>
<th>Age</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
<?php
$query=mysqli_query($db_con,'SELECT *
FROM `student_info`
ORDER BY `student_info`.`datetime` DESC;');
$i=1;
while ($result = mysqli_fetch_array($query)) { ?>
<tr>
<?php
echo '<td>'.$i.'</td>
<td>'.ucwords($result['name']).'</td>
<td>'.ucwords($result['department']).'</td>
<td>'.ucwords($result['age']).'</td>
<td><img class="rounded-circle avatar-xl" src="../../images/'.$result['photo'].'" ></td>';?>
</tr>
<?php $i++;} ?>
</tbody>
</table>
What am trying to achieve is to display only students in Agric Department

change your query from
SELECT * FROM `student_info` ORDER BY `student_info`.`datetime` DESC
to
SELECT * FROM `student_info` where department = 'Agric' ORDER BY `student_info`.`datetime` DESC

Related

Make email address in table clickable, opening send email [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
My table gets populated from my phpmyadmin database in php:
while($row2 = mysqli_fetch_array($result2))
{
$dataRow2 = $dataRow2."
<tr>
<th>Event Name</th>
<th>$row2[1]</th>
<th>Location</th>
<th>$row2[2]</th>
<th>Description</th>
<th>$row2[3]</th>
<th>Date</th>
<th>$row2[4]</th>
<th>Cost</th>
<th>$row2[5]</th>
<th>Email</th>
<th>$row2[6]</th>
</tr>";
}
And is in the HTML as so:
<table>
<?php echo $dataRow2;?>
</table>
I want to make the last th (the email address) clickable, opening a sendto for the user to email the address. How is this achievable?
Assuming the email is $row2[6], you would want to replace it with
<th>$row2[6]</th>
This will change it to a link with the email address still visible in the table.
You can use anchor tag mailto to clickable like the following
while($row2 = mysqli_fetch_array($result2))
{
$dataRow2 = $dataRow2."
<tr>
<th>Event Name</th>
<th>$row2[1]</th>
<th>Location</th>
<th>$row2[2]</th>
<th>Description</th>
<th>$row2[3]</th>
<th>Date</th>
<th>$row2[4]</th>
<th>Cost</th>
<th>$row2[5]</th>
<th>Email</th>
<th><a href='mailto:".$row2[6]."'>$row2[6]</a></th>
</tr>";
}

While Loop in php not working properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The While Loop is Only Displaying only Last entry
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
}
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>
";
?>
</table>
Your loop isn't printing anything to the page. It's setting values to variables. And it's over-writing those variable every time. So when the loop is done, only the last values are still set.
Then you just echo that last value. Once.
Instead, echo the output inside the loop so you can have one element of output for each loop iteration:
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
Note, however, that there are a couple of other things wrong here:
Your code is vulnerable to XSS attacks.
You are displaying user passwords. Never, ever do that. Your system shouldn't even have user passwords in a readable format. User passwords should be obscured using a 1-way hash and should never be retrievable by anybody.
As you have it, you are only printing a table row once, and using the last values of the row. All your loop does is assign values to $ID, $User, and $Password, and each loop pass just overwrites the old values. To fix this, you need to move the echo statements into the body of the loop.
This will let you print the current values over each iteration, instead of only printing the last. Here's the code that will work for what you want.
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
?>
</table>
Welcome to Stack Overflow #Cat I see two problems.
First: no echo, var_dump() or print found.
Second: the while loop will overwrite anything as long as it iterates, so in your case i would push to an array with each result found, like this:
<?php
$q = "SELECT id, user, password FROM sudents";
$fetch = mysql_query($q);
$final = [];
while ($row = mysql_fetch_array($fetch)) {
final[] = $row;
}
?>
<table>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>User Password</th>
</tr>
<?php foreach($final as $r) {?>
<tr>
<td><?= $r['id'] ?></td>
<td><?= $r['user'] ?></td>
<td><?= $r['password'] ?></td>
</tr>
<?php } ?>
</table>

Display table in PHP if statement [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can anyone help me how to display table inside PHP if statement.
Here's my code:
if( empty($errors))
{
ACTIVITIES \n
echo ' <table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td>$price_chicken</td>
<td>$num_chicken</td>
<td>$total_chicken</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td>$price_fish</td>
<td>$num_fish</td>
<td>$total_fish</td>
</tr>
</tbody>
.................
</table> ';
}
try this
<?php if( empty($errors)): ?>
ACTIVITIES \n
<table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td><?=$price_chicken;?></td>
<td><?=$num_chicken;?></td>
<td><?=$total_chicken;?></td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td><?=$price_fish;?></td>
<td><?=$num_fish;?></td>
<td><?=$total_fish;?></td>
</tr>
</tbody>
.................
</table>
<?php endif; ?>
You have the table code wrapped in single quotes, but there are PHP variables inside. PHP will not translate those variables unless you wrap the table in double quotes.
If this doesn't help, give us an idea of what the output looks like.
Try using a 2-dimensional array and looping through it to create a proper HTML table. This way, there's no hard-coding, so you can make as many rows/columns as you want. Here's the code, just change the array:
<table>
<tbody>
<?php
$tableArray = [["Chicken Feeding", $price_chicken, $num_chicken],
["Fish Feeding", $price_fish, $num_fish]];
foreach ($tableArray as $tableRow) {
echo "<tr>";
foreach ($tableRow as $tableCell) {
echo "<td>$tableCell</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>

Why not close row? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I output data from atabase in table.
In result table created with next code:
while($i=$res2->fetch_assoc()) {
$a++;
$t2.='
<tr>
<td>'.$a.'</td>
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>
';
if($valid!='id'){
$t2.='
<td>'.$partner.'</td>
';
}
$t2.='
<td>'.$http_referer.'</td>
</tr>';
}
$t1='
<table class="table table-hover table-bordered">
<thead>
<tr>
<th class="column_th_number">№</th>
<th>Date</th>
';
if($valid!='id'){
$t1.='
<th>Partner</th>
';
}
$t1.='
<th></th>
</tr>
<tr>
<td colspane="2">
</td>
</tr>
</thead>
<tbody>
';
$t3='
</tbody>
</table>
';
echo $t1.$t2.$t3;
but in the result I see that last row was not closed (see image):
Tell me please why last row was not closed?
And how can I make this right?
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>
if($valid!='id'){
replace with
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>'; <--- end the string here
if($valid!='id'){
you missed ';
<td>'.$a.'</td>
<td>'.date_format(new DateTime($i['date']),'d.m.Y').'</td>';
if($valid!='id'){
Thanks ALL.
Error was with colspan(value does not match the number of columns).
Thanks all for help

Retrieving records from mysql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Just looking for better solution.I'm retrieving all the records from mysql. Then I will send this data by email. Its working fine. Is there any better solution? thanks
$query = "SELECT * FROM order_details WHERE order_id = '".$data['order_id']."'";
while($row = mysql_fetch_array($result,MYSQL_ASSOC)) {
$message_admin .= "<table style='text-align:center;' border='1' cellspacing='0' cellpadding='7'>
<tr>
<td>Source</td>
<td>".$data['source']."</td>
</tr>
<tr>
<td>Email</td>
<td>".$data['email']."</td>
</tr>
<tr>
<td>Message</td>
<td>".$data['message']."</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>";
}
You would benefit from: http://us.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Instead of the complicated string manipulation you are doing now.
You are setting $row, but using $data?
I would take a look at the following to improve your code.
mysql-real-escape-string
SELECT * vs SELECT column

Categories