php foreach get all data from database table - php

I would like to get all the values of: echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>"; Some how it only returns the first on. After that i would like to calculate the sum of them.
At the moment the code only gets the first.
echo "<table class=\"zebra\">";
$sum1=0;
$sum2=0;
$numbering =1;
$query2 = "SELECT pisteet_1 As PIY, pisteet_2 as PIK, nimi As NIM, opisto As OPI, pisteet.kaupunki_id As KA FROM
pisteet INNER JOIN joukkueet ON joukkueet.id = pisteet.team_id INNER JOIN oppilaitokset ON oppilaitokset.opisto_id = joukkueet.opisto_id GROUP BY nimi, team_id ORDER BY team_id ASC";
foreach ($db->query($query2) as $row2) {
echo "<tr class=\"all " . $row2['KA'] . "\">";
echo "<td>" . $numbering . "</td>";
echo "<td>" . $row2['NIM'] ."<span>" . $row2['OPI'] ."</span></td>";
//--------should get all the points----------
echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>";
$sum1 +=$row2['PIY'];
$sum2 +=$row2['PIK'];
echo '<td class="Sum">'.$sum1.'/'.$sum2."</td>";
//-------------------------------------------
echo "</tr>";
$numbering ++;
}
echo '</table>';

Related

SQL result as txt.file by php [duplicate]

This question already has answers here:
Pulling data from SQL, and writing to a text file
(7 answers)
Closed 6 years ago.
I have a php file, which transform my sql query in a php output. Everything works fine. But now i need a output as txt.file and my code does not work.
Here the query:
<?php
$pdo = new PDO('mysql:host=localhost;dbname=DB', 'USER', 'PW');
$result = ("SELECT o.order_id AS Bestellnummer, o.customer_id AS Kundennummer, o.order_status_id AS Bestellstatus, op.quantity AS Anzahl, op.model AS Pharmacode, op.name AS Bezeichnung, p.location AS Lieferant, o.shipping_company AS Firma, o.shipping_firstname AS Vorname, o.shipping_lastname AS Nachname, o.shipping_address_1 AS Lieferadresse, o.shipping_postcode AS PLZ, o.shipping_city AS Ort
FROM oc_order o
INNER JOIN oc_order_product op
ON o.order_id = op.order_id
INNER JOIN oc_product p
ON op.product_id = p.product_id
WHERE o.order_status_id = 1 AND p.location = 1
ORDER BY o.order_id, op.model");
echo "<table border='0'>
<tr>
<th>Best.Nr</th>
<th>Kd.Nr</th>
<th>Status</th>
<th>Anz.</th>
<th>Pharmacode</th>
<th>Bezeichnung</th>
<th>Lieferant</th>
<th>Firma</th>
<th>Vorname</th>
<th>Nachname</th>
<th>Adresse</th>
<th>PLZ</th>
<th>Ort</th>
</tr>";
foreach ($pdo->query($result) as $row) {
echo "<tr>";
echo "<td>" . $row['Bestellnummer'] . "</td>";
echo "<td>" . $row['Kundennummer'] . "</td>";
echo "<td>" . $row['Bestellstatus'] . "</td>";
echo "<td>" . $row['Anzahl'] . "</td>";
echo "<td>" . $row['Pharmacode'] . "</td>";
echo "<td>" . $row['Bezeichnung'] . "</td>";
echo "<td>" . $row['Lieferant'] . "</td>";
echo "<td>" . $row['Firma'] . "</td>";
echo "<td>" . $row['Vorname'] . "</td>";
echo "<td>" . $row['Nachname'] . "</td>";
echo "<td>" . $row['Adresse'] . "</td>";
echo "<td>" . $row['PLZ'] . "</td>";
echo "<td>" . $row['Ort'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
I need the endcode for a output in txt or csv. I suppose that the initial code (SELECT...) has no clear output string to connect at a new code for the output.
You can try collect full string by PHP and than put it to file:
So you need create $string variable before loop and append each row to $string:
$string = "";
$file_path = "file.csv";
foreach ($pdo->query($result) as $row) {
$string .= $row['Bestellnummer'] . ',';
...
}
file_put_contents($file_path, $string)

Displaying multiple record on PHP

I want to display multiple records from single person in a table using PHP however the other records are outside the table and only one record is inside the table. It looks like this (view me).
Here is my code
$resultSet2 = $mysqli->query("SELECT class_subject, target_grade, current_grade , cl.classID FROM students AS stud INNER JOIN grade AS gr ON stud.studentID = gr.studentID INNER JOIN class cl ON gr.classID = cl.classID WHERE (cl.classID = '1' OR '2') and surname = '$search' ");
AND
while($row = $resultSet2->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['class_subject'] . "</td>";
echo "<td>" . $row['target_grade'] . "</td>";
echo "<td>" . $row['current_grade'] . "</td>";
echo "</tr>";
echo "</table>";
How can i put them inside the table like the first one?
Move the closing table tag outside the while loop </table> also close your while loop.
echo "<table>";
while($row = $resultSet2->fetch_array()) {
echo "<tr>";
echo "<td>" . $row['class_subject'] . "</td>";
echo "<td>" . $row['target_grade'] . "</td>";
echo "<td>" . $row['current_grade'] . "</td>";
echo "</tr>";
}
echo "</table>";

Fetch data from different tables using while loop and if statement

I am attempting to fetch data from my database using 8 different tables named:
inventory descriptorid typeid conditionid statusid locationid stationid users
I believe my query is working correctly the problem is using my $row variable. I am only getting the results from data in the inventory table. Can Someone explain how I can fetch my data from each table?
The code is below:
<?php
// get the records from the database
if ($result = $con->query("
SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname,
inventory.Serial, inventory.ServiceTag, inventory.CityTag,
conditioncid.conname, statusid.statusname, locationid.locname,
stationid.stationname, users.Fname, users.Lname,
inventory.PurchaseDate, inventory.POnumber
FROM inventory
LEFT JOIN descriptorid
ON inventory.Descriptor = descriptorid.dename
LEFT JOIN typeid
ON inventory.Type = typeid.tyname
LEFT JOIN conditioncid
ON inventory.ConditionC = conditioncid.conname
LEFT JOIN statusid
ON inventory.Status = statusid.statusname
LEFT JOIN locationid
ON inventory.Location = locationid.locname
LEFT JOIN stationid
ON inventory.Station = stationid.stationname
LEFT JOIN users
ON inventory.cuserFname = users.Fname
AND inventory.cuserLname = users.Lname "))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border id='myTable' class='tablesorter' >";
// set table headers
echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";
echo "<tbody";
while ($row = $result->fetch_object())
{
// dump whats returned
// print_r($row);
echo "<tr>";
echo "<td>" . $row->InventoryID . "</td>";
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->Serial . "</td>";
echo "<td>" . $row->ServiceTag . "</td>";
echo "<td>" . $row->CityTag . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
echo "<td>" . $row->PurchaseDate . "</td>";
echo "<td>" . $row->POnumber . "</td>";
echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
echo '<td><img src="img/default/button_mini_delete.gif">';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
You should name your row vars as you select them (as you did it with the columns of the inventory table):
Wrong:
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
Use instead:
echo "<td>" . $row->dename . "</td>";
echo "<td>" . $row->tyname. "</td>";
echo "<td>" . $row->conname. "</td>";
echo "<td>" . $row->statusname. "</td>";
echo "<td>" . $row->locname. "</td>";
echo "<td>" . $row->stationname. "</td>";
echo "<td>" . $row->Fname. "</td>";
echo "<td>" . $row->Lname. "</td>";
Are you sure that you want use LEFT JOIN instead of INNER JOIN? That means that if you have table A and B you only take elements which are located in A but not necessarily in B. That means if there is not a matching partner for an element of the left table, you would have a row containg an element of table A in one column and null in the other column.
This might be one reason for having a resultset only containing an element of the inventory table.
As far as I understand you want a result containing the elements joined on equal attributes, so try to replace LEFT JOIN with INNER JOIN.
Image source: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
Ok so the issue was instead of matching the third option (dename, tyname,etc.) on my secondary tables with the inventory I should've been matching it with the ID of that said table. Now when I query the third option with my row variable I can select any option from my secondary table (deid,tyid). It now outputs the desired results and hopefully I can create a drop down list using a similar query.Thanks for all the ideas.

PHP - Display SQL results in multiple tables

I have a database table with student room assignments. Each student has a specific hall, floor, and apartment. I need to display each student in a specific table so the results look like a floor layout. Below is an example. The student ID needs to be in the correct Apartment slot. There could be several ID's per apartment. Right now it just lists them down the page.
Apartment 102 Apartment 101
Apartment 104 Apartment 103
Apartment 106 Apartment 105
$query = "select res.ID_NUM as ID, res.APARTMENT
From Residents res
Where res.sess_cde = '$pulledsession'
and res.ROOM_ASSIGN_STS = 'A'
and res.BLDG_CDE = '$pulledhall'
and res.FLOOR = '$pulledfloor'";
$result = odbc_exec($connect, $query);
echo "<table style='padding:25;'>
<tr>
<th>Apartment</th>
<th>ID</th>
</tr>";
while(odbc_fetch_row($result)){
$ID = odbc_result($result,ID);
$APARTMENT = odbc_result($result,APARTMENT);
if ($APARTMENT == $pulledfloor.'01')
{
echo "<tr >";
echo "<td>" . $pulledfloor.'01' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'02')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'02' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'03')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'03' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'04')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'04' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'05')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'05' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'06')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'06' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
}
echo "</table>";
You would need to retrieve the room number as well. After that, one way to do it would be to load the results into an associated array:
$rooms[$row['roomNumber']] = $resName; // Example
Once you have the array built, then you can echo it back out into a table, either manually (build the full table and echo each one with
echo "<tr><td>".$rooms['102']."</td><td>".$rooms['101']."</td></tr>";
or similar, or do it dynamically by incrementing two room numbers in a loop.
If you have multiple students in a room, then tack on more depth to the array:
$rooms[$row['roomNumber']][] = $resName;
Then use a loop in each cell to echo it back out.

PHP select from two tables and join them?

I'm coding an car rental script in PHP, I have all the cars in one SQL table and all the rentals in another. How can I show all rentals right under the cars?
**Like this:**
bilar.reknr / Marke / Modell
starttid / stop tid
starttid / stop tid
starttid / stop tid
bilar.reknr / Marke / Modell
starttid / stop tid
starttid / stop tid
**EX**
ZOG-933 / BMW / 320
26.3.2013 / 27.3.2013
28.3.2013 / 29.3.2013
30.3.2013 / 31.3.2013
GOD-123 / MAZDA / 6
1.4.2013 / 2.4.2013
5.4.2013 / 6.4.2013
This is my code now:
<?php
include 'connection.php';
$sqlquery = "SELECT * FROM bilar";
$sqlarray = mysql_query($sqlquery);
print "<table border='1'><tr>";
print "<td>Register nummer</td>";
print "<td>Marke</td>";
print "<td>Modell</td>";
print "<td>arsmodell</td>";
print "<td>personer</td>";
print "<td>fel</td>";
print "</tr>";
while ($row = mysql_fetch_array($sqlarray)) {
print "<tr style='font-weight:bold;'>";
//print "<td>" . $row['reknr'] . "</td>";
print "<td>" . $row['marke'] . "</td>";
print "<td>" . $row['modell'] . "</td>";
print "<td>" . $row['arsmodell'] . "</td>";
print "<td>" . $row['personer'] . "</td>";
print "<td>" . $row['fel'] . "</td>";
print "</tr>";
$iddd=$row['reknr'];
$sqlquery = "SELECT * FROM hyr";
$sqlarray = mysql_query($sqlquery);
while ($row = mysql_fetch_array($sqlarray)) {
if ($row['reknr']=$row['reknr1']){
print "<tr>";
print "<td>" . $row['startdat'] . "-" . $row['stopdat'] . "</td>";
print "<td>" . $row['starttid'] . "-" . $row['stoptid'] . "</td>";
print "</tr>";
}
}
}
print "</table>";
mysql_close();
?>
You can try this
Suppose You have the following table structure:-
Car_table: car_id, car_name, car_color
Rental_table: rental_id, car_id, rental_date
So your query will be
$query = "Select Car_table.*,Rental_table.* from Car_table,Rental_table WHERE Car_table.car_id=Rental_table.car_id GROUP BY car_id"
Then rest I hope you know what to do. Hope this helps
include 'connection.php';
$sqlarray = mysql_query("SELECT t1.*, t2.* FROM bilar t1 LEFT JOIN hyr t2 ON t1.reknr = t2.reknr1");
$arr = array();
print "<table border='1'><tr>";
print "<td>Register nummer</td>";
print "<td>Marke</td>";
print "<td>Modell</td>";
print "<td>arsmodell</td>";
print "<td>personer</td>";
print "<td>fel</td>";
print "</tr>";
$arr =array();
while ($row = mysql_fetch_array($sqlarray)) {
$arr[$row[0]]['marke']=$row[1];
$arr[$row[0]]['modell']=$row[2];
$arr[$row[0]]['arsmodell']=$row[3];
$arr[$row[0]]['personer']=$row[4];
$arr[$row[0]]['fel']=$row[5];
$arr[$row[0]]['startdat'][]=$row[6];
$arr[$row[0]]['stopdat'][]=$row[7];
}
foreach($arr as $reknr=>$row) {
print "<tr style='font-weight:bold;'>";
//print "<td>" . $reknr . "</td>";
print "<td>" . $row['marke'] . "</td>";
print "<td>" . $row['modell'] . "</td>";
print "<td>" . $row['arsmodell'] . "</td>";
print "<td>" . $row['personer'] . "</td>";
print "<td>" . $row['fel'] . "</td>";
print "</tr>";
for($i=0; $i <= count($row['startdat']) -1 ; $i++) {
print "<tr>";
print "<td>" . $row['startdat'][$i] . "-" . $row['stopdat'][$i] . "</td>";
print "</tr>";
}
}
print "</table>";
mysql_close();

Categories