Not printing in correct columns - php

I have set up a for and while loop to print through the contents of my database for my menu system. It works fine, however, the places to where each of the table contents is displayed is one below where it should be. See this picture below:
The problem is that the items which are showing under "Main Courses" are supposed to be under the "Starters" section.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $countArray; $i++){
echo "<h1 id='starters' class='head-font text-center head'>$result_array[$i]</h1>";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo
"
<div id='hide'>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo
"<tr>" .
"<td>$name - <small>$description</small></td>" .
"<td class='text-right'>" . "£" . $price . "</td>" .
"</tr>";
}
echo "</tbody>
</table>
</div>";
}
for($j = 1; $j < $numRows+1; $j++){
echo $j;
}
// print_r($result_array[2]);
?>

Related

How to display database content as an html table?

I am trying to display all the data in my database in table format. I am using a while loop here. The first row looks good, but the subsequent rows don't display like a table.
<?php
require_once"../connect.php";
$sql = "SELECT * FROM history WHERE LOWER(trackingNo)=LOWER('$track') ORDER BY dates DESC";
$result = $con -> query($sql);
if ($result -> num_rows > 0) {
// output data of each row
echo "<table class='striped ex2 centered'>[The way my output looks right now][1]
< tr > <th class='trcol' > DATE < /th> <th class='trcol'>TIME</th > <th
class='trcol' > LOCATION < /th> <th class='trcol'>STATUS</th > <th
class='trcol' > REMARKS < /th></tr >";
while ($row = $result -> fetch_assoc()) {
$dates = $row['dates']; enter code here
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo" <tr><td>".$dates."</td> <td>".$tim."</td>
< td > ".$location." < /td> <td>".$status."</td >
<td>".$remarks." < /td></tr > </table>";
}
}
else {
echo "0 results";
}
$con -> close();
?>
This is how it currently looks like
The problem is that your closing table tag is inside the while-loop, so no iterations after the first will actually be part of the table. Edit your code to look like this:
while ($row = $result->fetch_assoc()) {
$dates = $row['dates'];
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo "<tr>";
echo "<td>".$dates."</td>";
echo "<td>".$tim."</td>";
echo "<td>".$location."</td>";
echo "<td>".$status."</td>";
echo "<td>".$remarks."</td>";
echo "</tr>";
}
echo "</table>";
You just need to take your </table> outside of the while loop.
So, it would be -
while ($row = $result -> fetch_assoc()) {
$dates = $row['dates'];
$tim = $row['tim'];
$status = $row['status'];
$remarks = $row['remarks'];
$location = $row['location'];
echo" <tr>
<td>".$dates."</td> <td>".$tim."</td>
<td> ".$location." </td> <td>".$status."</td >
<td>".$remarks." </td>
</tr>"; // <-- removed table closing
}
echo "</table>"; // <-- close table here

Output Multiple Database Results

I currently have this code set up:
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
$data_exist = true;
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
}
}
And then:
<?php if ($data_exist){?>
<p><?php echo $id ?></p>
<p><?php echo $teacher_set?></p>
<p><?php echo $name?></p>
<p><?php echo $description?></p>
<?php
}?>
However, the issue is if there is multiple results in the database it only outputs one of them, how can I prevent this from happening and output two?
I want to make it so every row has their own section, like this: http://prntscr.com/hcgtqn so if there is only one result, one one will show etc.
You have to echo data in a loop. Right now you are reassigning values in while($row = mysqli_fetch_assoc($result)) iterations and printing just the last one.
You need to print each time you read a row from the database.
about the styles, you can represent it in many ways. In the code below I present it in a table.
<table>
<thead>
<tr>
<th>id</th>
<th>teacher set</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM homework WHERE class = '$class'";
$result = mysqli_query($conn, $sql);
$data_exist = false;
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {
$id = $row["id"];
$teacher_set = $row["teacher_set"];
$class = $row["class"];
$name = $row["name"];
$description = $row["description"];
// you need to print the output now otherwise you will miss the row!
// now printing
echo "
<tr>
<td>".$id."</td>
<td>".$teacher_set."</td>
<td>".$name."</td>
<td>".$description."</td>
</tr>";
}
}
else // no records in the database
{
echo "not found!";
}
?>
</tbody>
</table>
</body>
</html>

Dynamically display for table with headings accordingly

Firstly, the headings are stored in h1 tags. This is taken from a separate table named "menu_type". Which is linked through a "menu" table.
I am trying to display data on the base like this:
HEADING
Table Data
2nd Heading
Second Data
--- In a loop until it is all completed ---
Here is a like page of what it is doing
I believe I have the methods correct and can see what it is doing, it is printing the first heading, then a blank table, then the second heading and then the data from the first table.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $numRows; $i++){
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>$result_array[$i]</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
<tr>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo "
<td>$name - <small>$description</small></td>
<td class='text-right'>£$price </td>
";
}
echo
"
</tr>
</tbody>
</table>
";
}
// print_r($result_array[2]);
?>
You don't need these anymore, it's like you're repeating the query. It looks incorrect also.
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
The menu items are already in this query, you just have to loop through it;
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
UPDATE 1: this code is incorrect, it doesn't insert the value to the array. I updated the code (after "try this").
$result_array[] = $menuType;
UPDATE 2: the $result is repeatedly used in mysqli functions, the index is being moved. What I did is copied the initial $result to $resultCopy. Try code again, haha
Use array_push($array,$value_you_insert) function, for inserting elements to an array.
Try this;
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$resultCopy = $result;
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($resultCopy)){
$menuType = $row['type'];
array_push($result_array,$menuType);
}
for($i = 0; $i < $numRows; $i++){
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>".$result_array[$i]."</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>Item</th>
<th class='text-right'>Price</th>
</tr>
</thead>
<tbody>
";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo"
<tr>
<td>".$name." - <small>".$description."</small></td>
<td class='text-right'>£".$price."</td>
</tr>
";
}
echo
"
</tbody>
</table>
";
}
?>

MySQL query only fetching the first row instead of all the rows in the table

I'm trying to fetch multiple rows in MySQL database but it is fetching only the first entry!
$data = mysql_query("SELECT * FROM twitter_tbl")
while($info = mysql_fetch_array($data))
{
$desc = $info['description'];
$screen_name1 = $info['screen_name'];
$final_oauth_token = $info['final_oauth_token'];
$final_oauth_token_secret = $info['final_oauth_token_secret'];
);
So I get only 1 row but I need to get multiple rows as every row has got its unique screen_name and description.
I don't know where I'm going wrong.
Correct code to this:
$data = mysql_query("SELECT * FROM twitter_tbl");
while($info = mysql_fetch_array($data))
{
echo $desc = $info['description'];
echo $screen_name1 = $info['screen_name'];
echo $final_oauth_token = $info['final_oauth_token'];
echo $final_oauth_token_secret = $info['final_oauth_token_secret'];
}
While statements are enclosed within {} not {);
Edited:
$data = mysql_query("SELECT * FROM twitter_tbl")
while($info = mysql_fetch_array($data))
{
Echo $desc = $info['description'];
Echo $screen_name1 = $info['screen_name'];
Echo $final_oauth_token = $info['final_oauth_token'];
Echo $final_oauth_token_secret = $info['final_oauth_token_secret'];
}
You simply need to place it in a container that would display the results on your webpage
Try This:
echo "<table>";
$data = mysql_query("SELECT * FROM twitter_tbl");
while($info = mysql_fetch_array($data))
{
$desc = $info['description'];
$screen_name1 = $info['screen_name'];
$final_oauth_token = $info['final_oauth_token'];
$final_oauth_token_secret = $info['final_oauth_token_secret'];
echo "
<tr>
<td>".$desc."</td>
<td>".$screen_name1."</td>
<td>".$final_oauth_token."</td>
<td>".$final_oauth_token_secret."</td>
</tr>";
}
echo "</table>";
Adding data into array.
what is your target? (Store data? echo?)
CODE:
$data = mysql_query("SELECT * FROM twitter_tbl");
if($data && mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array($data))
{
$desc[$i] = $info['description'];
$screen_name1[$i] = $info['screen_name'];
$final_oauth_token[$i] = $info['final_oauth_token'];
$final_oauth_token_secret[$i] = $info['final_oauth_token_secret'];
++$i;
}
unset($i, $info, $data);
}
Or like Echo mode
CODE:
$data = mysql_query("SELECT * FROM twitter_tbl");
if($data && mysql_num_rows($data) > 0)
{
$x = "
<table>
<tr>
<th>
desc
</th>
<th>
screen_name
</th>
<th>
oauth Token
</th>
<th>
secret oauth Token
</th>
</tr>";
//Warning it's eat a lot of memory
while($info = mysql_fetch_array($data))
{
$x .= "
<tr>
<td>
{$info['description']}
</td>
<td>
{$info['screen_name']}
</td>
<td>
{$info['final_oauth_token']}
</td>
<td>
{$info['final_oauth_token_secret']}
</td>
</tr>";
}
$x = "
</table>";
echo $x;
unset($info, $data, $x);
}

click on a link in a table, then check in what row the link was

What I want to make is this:
I am making a blog. Now I want a query in my mysql-database that finds all the information that is in there. After that it should echo the title's of the blog's on my site in a table.
Then I would like to give people a choice which blog they would like to see. So when they click on the first title they will see the content of that blog.
So now the problem is that I don't know how I can make href's on every title that all go to another php file. In that file it should know which row/title is clicked and then it should make another query in the database that will find the content of that title. Then it should echo it on the site.
I got the table finished. I also know how to make the href's to the other page. the only thing I need to know is which title/href is clicked.
Is it possible to make this with php only? if yes, please explain me how.
I hope I am clear in what i would like to make.
EDIT: this is my code so far:
<?php
session_start();
$connectie = mysql_connect('localhost', 'root', 'usbw');
if ($connectie == false){
echo 'Er is iets fout gegaan met de connectie van de database';
}
if (mysql_select_db('dldatabase', $connectie) == false) {
echo 'Er kon geen verbinding met de database gemaakt worden';
}
$query = "Select *
from forum";
$resultaat = mysql_query($query, $connectie);
echo "<table border='1'>
<tr>
<th>Tijd</th>
<th>Gebruikersnaam</th>
<th>Titel</th>
</tr>";
`while($row = mysql_fetch_array($resultaat))
{
$_SESSION['row'] = $row;
echo "<tr>";
echo "<td>" . $row['Tijd'] . "</td>";
echo "<td>" . $row['Gebruikersnaam'] . "</td>";
echo "<td>" . '<a class="formtitellink" href="forumdocument.php">' . $row['Titel'] . '</a>' . "</td>";
echo "</tr>";
}
echo "</table>";`
this is the blog page
and this is where the content of the blog should be±
session_start();
$row = $_SESSION['row'];
$row = $row + 1;
$query = "Select titel, inhoud
from forum
where `ID` = '$row'";
$resultaat = mysql_query($query, $connectie);
echo "$resultaat";
You can always use a framework like Wordpress, but if you want this kind of structure from scratch then you should read more about URL manipulation and use of GET parameters.
Simplest solution will be:
Create a page say post.php
Now every time you want a post to be displayed, send it with get
parameter. For eg: "page.php?id=hello-world"
On post.php, you can read this "id" parameter and then fire a
query accordingly.
Then print the resultant data on page.php
You need to pages like first page and the second page to open as hyper link.
Here is the sample code:
<?php
echo '<table width="650"><p><tr><th style="margin-left: 10px; padding-right: 70px;"></th>
<th style="width: 350px; ">Item for sale</th> <th>Location</th> <th>Posted</th></tr></table>';
$query1 = "select id from tblads order by priority desc LIMIT 10 OFFSET 1";
$result1 = mysql_query($query1);
$i1 = 0;
while ($row1 = mysql_fetch_row($result1))
{
$count1 = count($row1);
$y1 = 0;
while ($y1<$count1)
{
$c_row1 = current($row1);
$id = $c_row1;
next($row1);
$y1 = $y1 + 1;
//////TABLE
echo'<style> th{padding:0;}</style> ';
echo '<table style=" table-layout:fixed; order-collapse: collapse; " width="650">';
echo '<tr> <th> </th> <th style="width:350px;"> </th> <th style=""></th> <th style="width:100px;"></th></tr>';
$query = "select image,details, location, date from tblads where id=$id";
$result = mysql_query($query);
if (!$result) {$message = 'ERROR:' . mysql_error();return $message;}
else
{ $i = $i1;
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr> ';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td style="">' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo ' </tr>';
$i = $i + 1;
}
}
}
echo'</table>'; //Content TABLE CLOSE
}
mysql_free_result($result); mysql_close($con);
?>
Then the second page view.php to be opened by the hyperlink:
<?php
$id=$_GET['id'];
$query = "select image, details, location, price, date, id from tblads where id=$id";
$result = mysql_query($query);
if (!$result)
{
$message = 'ERROR:' . mysql_error();
return $message;
}
else
{ $i = 0;
echo '<table><p><tr>';
while ($i < mysql_num_fields($result))
{
$meta = mysql_fetch_field($result, $i);
echo '<th>' . $meta->name . '</th>';
$i = $i + 1;
}
echo '<th>View</th></tr>';
$i = 0;
while ($row = mysql_fetch_row($result))
{
echo '<tr>';
$count = count($row);
$y = 0;
while ($y < $count)
{
$c_row = current($row);
echo '<td>' . $c_row . '</td>';
next($row);
$y = $y + 1;
}
echo '<td></td> </tr>';
$i = $i + 1;
}
echo' </p></table>';
mysql_free_result($result);
mysql_close($con);
}
?>

Categories