Making table in PHP from database - php

I have a database called clanovi (members in englsih) which has stored name, surname, adress, email and gender of members. What I am trying to do is make a table from this data from database.
Here is my code https://jsfiddle.net/9j34xfwx/2/
<body>
<?php
require_once("Konekcija.php");
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("umv");
$sql=mysql_query("SELECT * FROM clanovi ORDER BY rbroj ASC");
$ime='ime';
$prezime='prezime';
$adresa='adresa';
$email='email';
$spol='spol';
?>
<table id='display'>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
<tr><td>Prezime clana: <?php echo $rows[$prezime]; ?></td>
<tr><td>Adresa clana: <?php echo $rows[$adresa]; ?></td>
<tr><td>Email clana: <?php echo $rows[$email]; ?></td>
<tr><td>Spol clana: <?php echo $rows[$spol]; ?></td>
<?php
}
?>
</table>
Here is photo of my web page when it lists all members
from database http://pho.to/ACgD1
My question is, how can I make a table so members name, surname, adress, email and gender are in columns, and beneath are the data from database. As you can see on the photo, my code adds one member beneath another member.

You're creating a new row for every value (and then never closing those rows, which the browser is attempting to correct for you):
<tr><td>Ime clana: <?php echo $rows[$ime]; ?></td>
^--- here
Just create the row once for each iteration of the loop:
<tr>
<td>Ime clana: <?php echo $rows[$ime]; ?></td>
etc.
</tr>

I assume that you don't need to repeat the text like "Ime clana:" (say label) in each Iteration, and need to set it as column header instead, cause that's the exact problem that the HTML <table> is designed to solve. Also if you need the label data to be redundant, then you must be defeating the purpose of using a table, IMHO.
You need to do something like this..
<table id='display'>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td></tr>
<?php
}
?>
</table>

Use this It's work for you.
<table id='display'>
<thead>
<tr>
<th>Ime clana:</th>
<th>Prezime clana:</th>
<th>Adresa clana:</th>
<th>Email clana:</th>
<th>Spol clana:</th>
</tr>
</thead>
<tbody>
<?php
while ($rows=mysql_fetch_assoc($sql)){
?>
<tr>
<td><?php echo $rows[$ime]; ?></td>
<td><?php echo $rows[$prezime]; ?></td>
<td><?php echo $rows[$adresa]; ?></td>
<td><?php echo $rows[$email]; ?></td>
<td><?php echo $rows[$spol]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>

Related

How to make sql image clickable on display board

I am trying to make the fetched images on a sql database clickable. They display fine but no-one can click them or download them and I'd like them to be able to. The script is below.
<?php
include_once 'connect.php';
$result = mysqli_query($conn,"SELECT * FROM Leaderboard");
?>
<?php
if (mysqli_num_rows($result) > 0) {
?>
<p>Leaderboard and guest photos as of October 20, 2020. Our apologies to our prior guests.</p>
<table class='table table-bordered table-striped'>
<tr>
<td>Date</td>
<td>Photo</td>
<td>Team Name</td>
<td># of Players</td>
<td>Escape Room</td>
<td>Time</td>
<td>Success</td>
<td>Game Master</td>
</tr>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["date"]; ?></td>
<td><?php echo "<img src='/teamphotos/".$row['photo']." ' width='250' height='200'>"; ?>. </td>
<td><?php echo $row["name"]; ?></td>
<td><?php echo $row["players"]; ?></td>
<td><?php echo $row["room"]; ?></td>
<td><?php echo $row["time"]; ?></td>
<td><?php echo $row["win"]; ?></td>
<td><?php echo $row["master"]; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
Probably you want that if the user left clicks the image, a download of that image starts. For that you only need to wrap an anchor tag <a> around the <img> with the attributes src and download.
<td><?php echo ''; ?></td>
This is related to HTML and not to PHP. Here you can read more about it:
https://www.w3schools.com/howto/howto_html_download_link.asp

Parsing XML from a URL address and converting into a table using PHP

I'm trying to take the following xml from this url http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml and make a table with the data. I've verified I can pull the data by using print_r($xml); but I can't get the data to dump into the table. This is what I've got so far, which is probably wrong. Can anyone help me out with the proper code to use?
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<table>
<thead>
<tr>
<col><span style="font-weight:bold">Day</span></col>
<col><span style="font-weight:bold">Time(Eastern)</span></col>
<col><span style="font-weight:bold">Reservoir Elev. (behind dam)*</span</col>
<col><span style="font-weight:bold">Tailwater Elev. (below dam)*</span></col>
<col><span style="font-weight:bold">Avg Hourly Discharge*</span></col>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET->ROW as $obs) :?>
<tr>
<td><?php echo $obs->obs_day; ?></td>
<td><?php echo $obs->obs_hr; ?></td>
<td><?php echo $obs->upstream_elev; ?></td>
<td><?php echo $obs->downstream_elev; ?></td>
<td><?php echo $obs->avg_hourly_discharge; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As the XML has 2 <RESULTSET> elements, the RESULTSET->ROW was just picking the first one. So change that to RESULTSET[1]->ROW and you will get the data your after.
You also need to ensure that you use the same case for each element name...
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>

How to stop php 'for loop' from creating multiple table headers

I am creating a table that fetches data from an SQL database using a PDO method. The data loads fine but the issue I'm having is that the 'for loop' I'm using is multiplying a (table header) after every (table row).
I am wondering what a possible solution the the issue could be.
Any help is appreciated, thanks!
Here is the code:
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
</table>
<?php }
?>
Up at the very top is the connection file that creates a connection to my local database and a statement that brings in the information I want to display from the database like so:
$result = $conn->prepare("SELECT * FROM events");
$result->execute();
Few possible solutions are
i) Put the header part and the table opening and closing tag outside the for loop. This will give you an empty table with headers if there is no data.
ii) Put an if condition and print headers only when i = 0, and put table tags outside the loop. This will give you an empty table with nothing if there is no data.
Edit: Method II (since you are learning)
<table id="eventstable">
<?php
for($i=0; $row = $result->fetch(); $i++){
if($i == 0){
?>
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php }//if statment ends here ?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
I would also suggest since you are learning, use better ways than for loop. Look at the php PDO manuals and see the use of while or foreach. It will help more.
Put in loop only, what should be looped, everything else should be outside of loop.
For example, your code could look like this
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
for($i=0; $row = $result->fetch(); $i++){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php
}
?>
</table>
try this :
<table id="eventstable">
<tr>
<th>Event ID</th>
<th>Event Name</th>
<th>Location</th>
<th>Date</th>
</tr>
<?php
foreach($result->fetch() as $row){
?>
<tr>
<td><?php echo $row['event_id']; ?></td>
<td><?php echo $row['event_name']; ?></td>
<td><?php echo $row['event_location']; ?></td>
<td><?php echo $row['event_date']; ?></td>
</tr>
<?php }
?>
</table>

My full data is not able to display on my web page from database

I wrote code to retrieve data from database and to print in form of tables on my web. I have stored 4 columns of data in my database but while retrieving it's only showing one column.
My database image
My webpage
My code:
<?php
$con = mysqli_connect("localhost", "root", "", "project");
if(!$con)
{
die('not connected');
}
$result= mysqli_query($con, "SELECT name, stay, food, travel,
SUM(stay + food + travel) AS totalamount,doj
FROM placedetails ");
?>
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<?php
while($row =mysqli_fetch_array($result))
{
?>
<tbody>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
</tbody>
<?php
}
?>
</table>
</div>
</div>
Can anyone can tell where the mistake is?
And one more question: I want to diplay only the recent uploaded data on my web page. Suppose I have 4 names as mumbai, but uploaded at different times, I want to display the most recently added mumbai name on my web page
Can anyone help me out in this matter? I will be very thankful..
update your code as move tbody from php loop
<div class="container">
<table class="table table-hover">
<thead>
<tr>
<th>place</th>
<th>stay cost</th>
<th>food cost</th>
<th>flight cost</th>
<th>Date of journey</th>
<th>Total cost</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food'] ;?></td>
<td><?php echo $row['travel'] ;?></td>
<td><?php echo $row['doj'] ;?></td>
<td><?php echo $row['totalamount'] ;?></td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
1) <tbody> should be outside of while loop
2) if you want show only one record means no need to use while loop
3) if you want show recent record means just do descending sort by date column or id column
Query :
SELECT name, stay, food, travel, SUM(stay + food + travel) AS totalamount,doj FROM placedetails order by doj desc limit 1;
Table :
<tbody>
<?php
$row = mysqli_fetch_assoc($result); //fetch first record set only
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['stay']; ?></td>
<td><?php echo $row['food']; ?></td>
<td><?php echo $row['travel']; ?></td>
<td><?php echo $row['doj']; ?></td>
<td><?php echo $row['totalamount'];?></td>
</tr>
</tbody>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

Categories