Printing data horizontally using php and mysql - php

I want to print data from the database in a horizontal manner.
I have two table one that holds products names and another that holds products performance by months eg i want data to appear in a table like this
product name,performance by months from january to december
eg
product A,1000 ,2000, etc performance by months
product B,2000,3300, etc performace by months

Edit: I didn't realize you said you have two tables. So the query in my solution should be adapted with a JOIN and ordered, but we cannot dig further into this without knowing your schema. My solution addresses the main concern (i.e. printing results horizontally), provided you obtain two fields to show in two different rows.
Just retrieve your data and store it in a multidimensional array, THEN create the table.
$data = array();
$sql = "SELECT product, performance FROM table";
$rs = mysql_query($sql);
while ($row = mysql_fetch_assoc($rs))
{
$data[] = array($row['product'], $row['performance']);
}
echo "<table><tr>";
// print products in the first line of the table
foreach($data as $d)
{
echo "<td>" . $d[0] . "</td>";
}
echo "</tr><tr>";
// then print performances
foreach($data as $d)
{
echo "<td>" . $d[1] . "</td>";
}
echo "</tr></table>";

Related

Get proper column ids of the sql query passed through a link

Being a beginner in PHP, I have a table in a database which consists of 14 columns. I need to extract some of the columns through an 'href' link on another page. I am having a hard time trying to get the specific column ids for the specific column as I need the columns to be displayed as plain text separately on an html page.
So this is the fetch code to display columns 'A' and 'G' including two links in a table.
while($row=mysql_fetch_array($res))
{
echo "<tr>";
echo "<td>" . $row['A'] . "</td>";
echo "<td>" . $row['G'] . "</td>";
echo "<td>FASTA</td>";
echo "<td>Full Entry</td>";
echo "</tr>";
}
I am facing problems to get the columns A to M separately on the next php page of FullEntry.php and I need the data from the columns as plain text.
This is what I could come up with on FullEntry.php page.
$row = $_GET['rowid'];
<!--Here is where I need the multiple row ID's to get separate columnar data-->
echo $row;
?>
So How can use different id's for different columns from the original.php page to display results separately through the FullEntry.php page or If there can be any modifications with sql query as well. Help will be appreciated.
Any help would be appreciated.
Thank you in advance!
HereI have added a delimiter | bewteen $row reults like
echo "<td>Full Entry</td>";
And the result will be like Fullentry.php?rowid=a|b|c|d|e..... and you can access this by exploding the rowid.
$result = $_POST['rowid];
$result = explode("|",$result);
echo $result [0];
echo $result [1];...

Organise seats into tablerows in html output with PHP

I'm putting together something that's mean to allow for a user to book seats for a cinema showing. The row and seat numbers for every showing are stored in the database. I'm currently extracting them in the following method so that users can click on a seat button to select that seat for their booking:
echo "<form>";
echo "<table>";
while($row = mysqli_fetch_assoc($result)){
$rownum = $row['row'];
$seat = $row['seat'];
echo "<tr><td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td></tr>";
}
echo "</table>";
Right now this just outputs html showing all of the buttons as a single row in the table. I'd like the output to show seating across a single table row for every one of the table rows in the cinema screen. I'm not sure how to do this exactly given that each row is of differing lengths. E.g row A has twelve seats while row C has eight.
What would be the best way of accomplishing this?
You could easily update your code so that you will get a new table row every time the mysql row has another value. One thing to note is that you might want to add (depending on whether you're already sorting your results) the following ORDER BY row,seat.
echo "<form>";
echo "<table>";
echo "<tr>";
while($row = mysqli_fetch_assoc($result)){
if (!isset($oldrownumber)) $oldrownumber = $row['row'];
else if ($oldrownumber != $row['row']) {
echo "</tr><tr>";
$oldrownumber = $row['row'];
}
$rownum = $row['row'];
$seat = $row['seat'];
echo "<td><button type=\"submit\" name=\"seatsel\" value=\"$rownum$seat\">$rownum$seat</button></td>";
}
echo "</tr>";
echo "</table>";

Add check-box to remove from database in for each loop

I'm learning PHP at the moment, started a practice project to make a todo list.
The list allows the user to enter data into the database with an input field, and uses a while and foreach loop to display the data from the database.
I want to add a check-box to each item displayed that allows the user to check what items on the list they'd like to remove, and for the check-box to have a value corresponding to the id column of the item, then I'll add a submit button that will clear the checked items.
The database table I'm using has two columns an auto increment id column, and a description column.
Here's the loop:
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
foreach($row as $list_item) {
echo "<td>" . $list_item . "</td>";
}
echo "</tr>";
endwhile;
?>
I tried this:
foreach ($row as $id => $description) {
echo "<td>" . $id . $description . "</td>";
}
But for soeme reason this returns the column name, and the values like so:
id1 descriptionTodo List Item Number One.
id2 descriptionTodo List Item Number Two.
id3 descriptionTodo List Item Number Three.
id5 descriptionTodo List Item Number Four.
Can anyone set me on the right path?
I've got the project uploaded onto github if anyone wants to see the whole lot.
Thanks in advance for any help and suggestions.
I don't understand why you are using foreach actually , I would just do this :
<?php
$query = "SELECT * FROM list_data;";
$list = $mysqli->query($query);
while ($row = $list->fetch_array(MYSQLI_ASSOC)):
echo "<tr>";
echo"<td><input type='checkbox' value='".$row['id']."'</td> <td>".$row['description']."</td>";
echo "</tr>";
endwhile;
?>

How to use a mysqli result row more than once in PHP?

I am trying to put together a tool to help me with my upcoming fantasy hockey draft while also learning PHP. I am trying to create multiple lists on a page, one that displays the top 10 available players overall and then others that display the top 10 available players by position.
Here is my SQL query/code
include 'db/connect.php';
$sql='SELECT * FROM players WHERE pick IS NULL';
$players=$conn->query($sql);
if($players === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
} else {
$rows_returned = $players->num_rows;
}
Then later in the page I have a while loop that generates a table with the top ten players
while ($row = $players->fetch_array()) {
if ($i == 10) {
break;
}
echo "<tr>";
echo "<td>" . $row['Rank'] . "</td>";
echo "<td>" . $row['Player'] . "</td>";
echo "<td>" . $row['Team'] . "</td>";
...
And all that works fine. However, when I go to use the same method to generate a list containing only a certain position (C, RW/LW, etc...) it starts off where the top 10 player list ends. (See what I mean here: http://i.imgur.com/JApeftU.png)
I assume this has to do with the $players->fetch_array() however I do not know what the best way would be to get around it.
Any ideas?
Thanks!
Populate rows with all the players.
while ($row = $players->fetch_array()) { //→ $rows = $players->fetch_all();
$rows[] = $row;
}
You can use count() to get total amount of players in the array
$totalPlayers = count($rows);
Now you can loop through the array with for loop
for($i = 0; $i < $totalPlayers; $i++){
//echo out the stuff you want
echo $rows[$i]['player'];
}
Or only ten
for($i = 0; $i < 10; $i++){
//echo out the stuff you want
echo $rows[$i]['player'];
}
Well, for the future visitors, lured by the misleading title, the other answer is okay.
While for you personally, the other answer, as well as your question, is wrong.
And it's your idea on using databases is wrong in the first place.
A database is not like a text file, which you but bound to read every time from first line to last. Databases are quite intelligent and intended to return you the very data you requested.
Think it this way: what if your league will grow up to employ thousands of players. It will burden PHP script with lots of useless info, when it needs only a hundred of players.
So, it seems you need different queries to get differen data sets. First, you need a query
SELECT * FROM players WHERE pick IS NULL ORDER BY field DESC LIMIT 10
To get overall top ten, where field is whatever field you're using to determine the "top" player. And then several queries, each getting players for the certain position.
SELECT * FROM players WHERE pick IS NULL AND position=? ORDER BY field DESC LIMIT 10

Display results from MySQL query into table using PHP

This is for a timetable and what it does is displays the previous day's timetable and who has booked each slot (this is for a radio station.)
At the moment it displays who has booked each slot in chronological order however I would like it to say the time next to each result. The query outputs 24 rows (which is from Midnight to 23:00) and next to each slot I would like it to say (00:00, 01:00, 02:00, 03:00... 21:00, 22:00 so on so forth.)
This is my current code:
<?php
include("../config.php");
#// Timetable Clearup Variabls
$yesterday = strtotime('yesterday');
$yesterdow = date('l',$yesterday);
echo "<table width=\"580px\" class=\"board\" border=\>";
$order = "SELECT * FROM timetable WHERE day = '$yesterdow'";
$result = mysql_query($order);
// Error checking
if (!$result) {
// output error, take other action
}
else {
while ($row=mysql_fetch_array($result)){
// Append all results onto an array
$rowset[] = $row;
}
}
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td></tr>";
}
?>
Can you help?
I think we're all looking too hard at a VERY simple problem. You are already using SELECT * in your query, so you're already fetching all three columns from your table. So now, all you need to do is add another cell to each row of your table.
echo "<tr><td>" . htmlspecialchars($row['username']) . "</td><td>" . htmlspecialchars($row['time']) . "</td></tr>";
And to make sure you are fetching your rows in the correct order, you should add an ORDER BY to your query:
SELECT * FROM timetable WHERE day = '$yesterdow' ORDER BY time
If you don't specify an ORDER BY clause, you have no guarantee that you will get the results in any particular order.
And one last thing, you are looping through the rows twice, unnecessarily. Get rid of the foreach loop and put the echo directly inside the while loop.
try this:
foreach ($rowset as $row) {
echo "<tr><td>" . htmlspecialchars($row['username']) . htmlspecialchars($row['time'])"</td></tr>";

Categories