display more than one record >curdate - php

Attempting to display different data in a table, but am only gettting the most recent result. The only thing I have changed in my code is adding the >CURDATE rather than the most simple *. it has stopped displaying all of the results. can you help?
$result = mysqli_query($con,"SELECT * FROM tripdata WHERE date > CURDATE()");
while($row = mysqli_fetch_assoc($result))
{
echo "<table class='mainpics'>";
echo "<tr><td><a class='mainpic' href='". $row['pageurl'] ."'><img class='mainpic' src='" . $row['mainpic'] . "'/></a></td></tr>";
echo "</table>";
}
mysqli_close($con);
?>

Related

Multiple Tables of same Database on the same site

I want to display 9 different tables from my sql database in 9 different html created tables on the website.
In detail: I have 9 tables ("dt_bookmarks_01", "dt_bookmarks_02" etc.) with 4 columns "id" (which is primary and auto increment), icon (for favicon), link (url) and text (for the display text).
I've created 9 different html tables with bootstrap and want to output the content of each table in a different bootstrap table of my site.
My problem is that i have no idea how to get different "foreaches" or counter for each different table.
To automaticaly add new rows to the bootstrap table I use the count and foreach function. problem here is: I dont know how to seperate them from each other. If i have 4 entries in sql table 1 it multiplies the one and only entrie of sql table 2 to match the current count of 4.
I am very new to sql and php so I guess I just miss some fundamental functions or something.
document header:
php
$sql = "
SELECT *
FROM dt_bookmarks_01, dt_bookmarks_02";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
and for the html table I use:
php
<tbody>
<!--begin: SQL Selection -->
<?PHP
$count = 0;
foreach($rows as $item){
if (!empty($item['icon'])) {
$icon = '<img src="assets/media/bm-icons/'. $item['icon'] . '">';
}else{
$icon = '<img src="assets/media/bm-icons/default.png">';
}
$count++;
echo "<tr>";
/*echo "<td>" . $count . "</td>";*/
echo "<td> " . $icon . "</td>";
echo "<td> <a href=\"" . $item['link'] . "\"'>" . $item['text'] . "</a> </td>";
echo "<td></i> ";
echo "</i></td>";
echo "</tr>";
}
?>
<!--end: SQL Selection -->
</tbody>
I do not have a database on hand to give you an answer with complete code, but here is the idea:
<?php
for ($i = 1; $i <= 9; $i++)
{
$query = "SELECT index1,index2 FROM dt_bookmarks_0$i";
echo "<h1>This is the content of table $i</h1>";
# RUN THE QUERY HERE !!!
echo "<table>";
# EXTRACT THE RESULTS
foreach $rows as $item
{
echo "<tr><td>$item[index1]</td><td>$item[index2]</td></tr>"
}
echo "</table>";
echo "<br><br>";
}
?>
Loop on your tables.
In each table loop, you output the HTML code to display it's content.
Avoid SELECT *, specify your indexes (research "sql why avoid SELECT *")
So you loop twice. One time to go through the tables, the other to loop on the results.
so here is the new working code.
header:
<?PHP
require_once('/htdocs/_nt/mysql/data.php');
$sql = "
SELECT *
FROM dt_bookmarks";
$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;
}
$conn->close();
?>
and for the table output:
<?PHP
$count = 0;
foreach($rows as $item){
if ($item['category'] == talk) {
$count++;
echo "<tr>";
echo "<td> " . $icontalk . "</td>";
echo "<td> <a href=\"" . $item['url'] . "\"'>" . $item['text'] . "</a> </td>";
echo "</tr>";
}else{
echo "";
}
}
?>

How to display the output in a vertical view dynamically?

I am just running a SQL query to fetch a particular data from the database and would like to display in horizontal way inspite of the vertical format, Can we do something to display the data as required.
For Getting the clear understanding of the question i have attached the Output which i am getting and the output which i required after the SQL query is executed.
Thanks in Advance.
Query:
<?php
include 'connect-db.php';
$query = "SELECT distinct work_component FROM daily_progress_demo WHERE package_no='$package_no'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "<center>".$row['work_component']."</center>";
echo "<br />";
}
?>
you can do like this
echo "<table>"
echo "<tr>"
while($row = mysql_fetch_array($result)){
echo "<td>".$row['work_component']."</td>";
}
echo "</tr>";
echo "</table>";
echo $row['work_component']."<br>";

Displaying 2 records in a column using php

So I have a code
<?php
$showorder = "SELECT order_number FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
$ord = mysqli_fetch_array($orderesult);
?>
in my database customer number 522 has 2 order numbers, when i tried to show the result, it only shows 1.
Here's my other code
echo "<table>";
echo "<th>Order Number</th><th>Order date</th>";
echo "<tr><td>";
echo $ord["order_number"];
echo "</td><td>";
echo $ord["order_date"];
echo "</td></tr>";
You just need to use while() here for getting all records, something like:
while($ord = mysqli_fetch_array($orderesult)){
//echo all value here
}
Also note that, if you want to print $ord["order_date"] than you must need to select column also in your query.
Otherwise, $ord will only contain order_number value.
Your SQL is missing the extra column.
Current SQL:
SELECT order_number FROM orders WHERE customer_number=522
Change to:
SELECT order_number, order_date FROM orders WHERE customer_number=522
Put mysqli_fetch_array($orderesult); in a while loop.
while($ord = mysqli_fetch_array($orderesult)) {
echo $ord["order_number"];
# code
}
Replace your code with the below code and then try again
<?php
$showorder = "SELECT order_number, order_date FROM orders WHERE customer_number=522";
$orderesult = mysqli_query($con, $showorder);
echo "<table>";
echo "<tr>";
echo "<th>Order Number</th><th>Order date</th>";
echo "</tr>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr>";
echo "<td>$ord['order_number']</td>";
echo "<td>$ord['order_date']</td>";
echo "</tr>";
}
echo "</table>";
?>
echo "<table>";
echo "<th>Order Number</th>";
while($ord = mysqli_fetch_array($orderesult)) {
echo "<tr><td>";
echo $ord["order_number"];
echo "</td></tr>";
}
you must use loop to show all result , and you can use echo one time .
while($ord = mysqli_fetch_array($orderesult)) {
echo "<table>
<th>Order Number</th><th>Order date</th>
<tr><td>".
$ord["order_number"]."
</td></tr>";
}

How can I use hyperlinks in PHP to display data on the page it sends you to?

I have this piece of code below. It displays the image and name of all the entries in a table in my database. The name is set up to become a hyperlink.Is it possible to make it so when one specific name is clicked that data for only that specific name will be displayed on the page you are sent to?
So for example if I select the first entry that is displayed back "mealname1" and it takes me to the showrecipe.php page, can I make it so I can display all the data I have for "mealname1" and only "mealname1". I'm really lost, I have scoured the internet and my php books but can't find anything to that is relevant.
If there is no way of doing it is there an obvious solution that I am missing?... I am very much a novice to this... thanks for your help guys.
<?php
require("db.php");
$prodcatsql = "SELECT * FROM recipes";
$prodcatres = mysql_query($prodcatsql);
$numrows = mysql_num_rows($prodcatres);
if($numrows == 0)
{
echo "<h1>No Products</h1>";
echo "There are no recipes available right now.";
}
else
{
echo "<table id='recipetable'>";
while($prodrow = mysql_fetch_assoc($prodcatres))
{
echo "<tr>";
if(empty($prodrow['image'])){
echo "<td><img
src='./images/No_image.png' alt='"
. $prodrow['mealname'] . "'></td>";
}
else {
echo "<td><img src='./images/".$prodrow['image']
. "' alt='"
. $prodrow['mealname'] . "'></td>";
}
echo "<td>";
echo ''.$prodrow['mealname'].'';
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
Change the query to
SELECT * FROM recipes WHERE mealname='$mealname' LIMIT 1;
You can remove "LIMIT 1" if you want but this makes sure you will only get 1 or 0 row back. Don't forget to escape the string.

Table not displaying the first record from database

When I add my first record into the database table, it doesn't show on the page where the records are displayed. But when I add the second record and on, they are displayed on the page except the first record that I entered.
Here is my code:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
while ($row = mysql_fetch_array($result))
{
echo "<table>";
echo"<tr><th><B>Player Name</B><Th><B>Role</B></TR>";
while ($myrow = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>". $myrow['player_name']. "</td>";
echo "</td>";
echo "<td>" .$myrow['player_role']. "</td>";
echo "</tr>";
}
echo "</table>";
}
Can someone please tell me what is wrong?
It may be caused by nested while() loop. No need to use nested while(). Use one while() instead. Example:
$result = mysql_query("SELECT * FROM members ORDER BY player_role DESC", $db);
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysql_fetch_array($result))
{
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";
Player Name and Role should not be inside while() loop.
MOST IMPORTANT Do not use mysql, it is deprecated. Instead use mysqli
Your code (changed)
$result = mysqli_query($db,"SELECT * FROM members ORDER BY player_role DESC");
echo "<table>";
echo"<tr><th><B>Player Name</B></th><th><B>Role</B></th></tr>";
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<tr><td>'.$row['player_name'].'</td><td>'.$row['player_role'].'</td></tr>';
}
echo "</table>";

Categories