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();
Related
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>";
So I am trying to do this..
$userID = $_SESSION['user_session'];
$stmt = $this->db->prepare("SELECT * FROM orrs");
$stmt->bindparam(":id", $userID);
$stmt->execute();
$count = $stmt->rowCount();
echo
"<div class='table-responsive'>
<table class='table' border='1'>
<tr class='head'>
<h3>Snapshot</h3>
<th>Se</th>
<th>#s</th>
<th>Ae</th>
<th>Prt</th>
<th>Pin</th>
</tr>";
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.
Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..
I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts
//The current one
$stmt = $this->db->prepare("SELECT * FROM orrs");
//The get the count for stage '1'
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
$stage_1_count = $stage_1_stmt->rowCount();
//The get the count for stage '2'
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
$stage_2_count = $stage_2_stmt->rowCount();
You can execute these others to get the counts which you should use in place of $count in your loop.
Your while loop then becomes
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $stage_1_count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $stage_2_count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
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>';
I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);
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.