I have a mysql table, one of its columns called parking space has a number in it. However, for some records the 'parking space'-column is empty. Now, I want to call just this column in a page table where the column heads are numbered from 1 - 200 (first column: 1; second column: 2;....) So, if there is the value '12' in the 'parking space'-column, then this shall show up in column '12' and so on, and if there is no entry for a number then the column in the page table shall be left empty. How can I associate the numbers in 'parking space' with that page table?
...
<?php
$pagetable=array('1','2','3','4','5','6');//...until 200
foreach($pagetable as $value){
?>
<table border="1px" cellspacing="1" cellpadding="1">
<tr>
<th>
<?php echo $value ?>
</th>
</tr>
<?php
}
include("dbinfo.inc.php");
include_once("config.php");
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results = mysql_num_rows($result);
if ($results > 0){
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$park=mysql_result($result,$i,"park");
?>
<tr>
<td style="background-color:;">
<?php echo $park; ?>
<br>
<?php if($park!=''){ ?>
<?php echo $id; ?>
<?php
}
?>
</td>
</tr>
<?php
$i++;
}
}
?>
...
There are two ways, you can do this.
Create all 200 rows on the database table and query it. This will omit almost all of the headache to get what you want.
Do a for loop from 1 to 200 and compare the active item if it exists on the mysql results. For this you require an continuous numeric entry in the database to identify each columns individually.
What I mean is? Column 1 in the should have corresponding record indicating 1 somewhere in its record.
Here is an example of this:
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results = mysql_num_rows($result);
if ($results > 0) {
$num=mysql_numrows($result);
$rows = array();
while($row = mysql_fetch_row($result)) {
$rows[$row['id']] = $row;
});
//Now process it
}
Try this:
<?php
include("dbinfo.inc.php");
include_once("config.php");
$spaces = range(1, 200);
$results = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results_count = mysql_num_rows($result);
if ($results_count > 0) {
for ($i = 0; $i < $results_count; $i++) {
$parks[] = mysql_result($results, $i, "park");
}
}
?>
<table>
<tr>
<?php foreach ($spaces as $space): ?>
<td><?php echo $space ?></td>
<?php end foreach ?>
</tr>
<tr>
<?php foreach ($parks as $park): ?>
<td><?php if ($park > 0) { echo "$space"; } else { echo " "; } ?></td>
<?php end foreach ?>
</tr>
</table>
Start with this and tell me what you get.
Ok, I got it changed a little. Now it ahows me all from 1-200 and it shows the parking spaces values, but these values are not associated with the numbers. it looks to me like it puts them according to the id, for example in my table the customer on parking space 165 has the id 4 and therefore it doesnt put 165 to 165 but 165 to 4
<?php
include("dbinfo.inc.php");
include_once("config.php");
$spaces = range(1, 200);
$results = mysql_query("SELECT * FROM contacts");
$results_count = mysql_num_rows($results);
if ($results_count > 0) {
for ($i = 0; $i < $results_count; $i++) {
$parks[] = mysql_result($results, $i, "park");
}
}
?>
<table>
<tr>
<?php foreach ($spaces as $space){ ?>
<td><?php echo $space; ?></td>
<?php } ?>
</tr>
<tr>
<?php foreach ($parks as $park){ ?>
<td><?php if ($park!='') { echo $park.'<br>';} else { echo " "; } ?></td>
<?php }?>
</tr>
</table>
Related
for a project in HTML,CSS,PHP and JS I have to print the cells of a table according to the elements of a database. For example, if a cell is property of user1#test.com I want that the cell is blue, because in the database the colour for the user1 is blue. I have associated with success the user with the colour in the database, but I'm not able to scan every cells of the table.
In fact, my code is able to colour in blue just one cell of a single user, even if the user1 has more than one prenotation. This is my code:
$result = mysqli_query($db, "SELECT * FROM users");
$users=$result->fetch_assoc();
foreach ($users as $result){
$result = mysqli_query($db, "SELECT * FROM prenotations WHERE username='user1#test.com'");
$info_prenotations=$result-> fetch_assoc();
$result = mysqli_query($db, "SELECT * FROM colors WHERE username='user1#test.com'");
$info_colour=$result-> fetch_assoc();
$result = mysqli_query($db, "SELECT username from prenotations where username='user1#test.com'");
$info_username=$result->fetch_assoc();
}
And this is the cose for the table:
<table border="0" summary="Week">
<tr>
<th></th>
<th>Mon</th>
<th>Thur</th>
<th>Wed</th>
<th>Tues</th>
<th>Fri</th>
</tr>
<?php for ($i=1; $i<=11; $i++) { ?>
<tr>
<th>Slot <?php echo $i; ?></th>
<?php for ($j=1; $j<=5; $j++) { ?>
<td style="<?php if (intval($info_prenotations["slot"]) == $i && intval($info_prenotations["day"]) == $j) { echo "background-color: ".$info_colour["colour"].";"; } ?>"></td>
<?php } ?>
</tr>
<?php } ?>
</table>
Now I want to use the foreach loop in a way that I can scan the entire table to print the different cells for the different users. I suppose that my error is in the foeach loop, which I don't understand very well. Someone can help me, please?
I want to fill in the table. The database is filling the array, but this method didn't work. What is the problem? How to use foreach loop?
<?php
$a = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.ustsayfaid");
$a->execute(array());
$b = $a->fetchALL(PDO::FETCH_ASSOC);
$c = $a->rowCount();
$q = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.altsayfaid");
$q->execute(array());
$w = $q->fetchALL(PDO::FETCH_ASSOC);
$e = $q->rowCount();
if($c){
foreach($b as $w and $m as $n){
?>
<tbody>
<tr>
<td>
<?php echo $m["sayfa_adi"];?>
</td>
<td>
<?php echo ($n["sayfa_adi"]);?>
</td>
</tr>
<?php
}
}
?>
</tbody>
Here's what I came out with.
<?php
$firstQuery = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.ustsayfaid");
$firstQuery->execute(array());
$firstQueryResults = $firstQuery->fetchALL(PDO::FETCH_ASSOC);
$firstQueryCount = $firstQuery->rowCount();
$secondQuery = $db->prepare("select *
from sabit_sayfalar
inner join alt_sayfa on sabit_sayfalar.sayfa_id = alt_sayfa.altsayfaid");
$secondQuery->execute(array());
$secondQueryResults = $secondQuery->fetchALL(PDO::FETCH_ASSOC);
$secondQueryCount = $secondQuery->rowCount();
if ($firstQueryCount > 0 && $secondQueryCount > 0) {
?>
<tbody>
<?php foreach ($firstQueryResults as $firstQueryKey => $firstQueryRow) { ?>
<tr>
<td>
<?php echo $firstQueryRow["sayfa_adi"];?>
</td>
<td>
<?php echo ($secondQueryResults[$firstQueryKey]["sayfa_adi"]);?>
</td>
</tr>
<?php
}
}
?>
</tbody>
I changed the names of the variables for readability, and I adapted the table for using the information you provided... there are still some flaws connected with indexes that might be different for each SELECT, but that is another problem.
I also interpreted some variables that were missing as the variables you had but, for some reason, wrote wrong.
you cannot be used and in a foreach i think you want to use 2 foreach here and your <tbody> should be out of the loop
I am a little bit confused about using while loop inside another. Everything is fine with the second loop but the first one only returns one value and it is the same.
$aukciono_laimetojai_while = mysql_query("SELECT id, user_id, date, win
FROM tb_auction_winners WHERE user_id = 206");
$aukciono_istorija_while = mysql_query("SELECT user_id, aukciono_id,
COALESCE(SUM(bid), 0) AS bid, date FROM tb_aukciono_istorija
WHERE user_id = 206 GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_istorija_while)) {
while ($r2 = mysql_fetch_assoc($aukciono_laimetojai_while)) { ?>
<tr>
<td><?php echo $r2['date']; ?></td>
<td><?php echo $r2['win'] - $r1['bid']; ?> Eur</td>
<td>0 Eur</td>
<td>Plačiau <?php echo $r2['win'] . ' - ' . $r1['bid']; ?></td>
</tr>
<?php } } ?>
$aukciono_laimetojai_while returns:
click here
$aukciono_istorija_while returns: click here
Using these 2 while loops, the table looks like this: (www.i.stack.imgur.com/Os8Rx.png) (can't use more than 2 links, sorry)
Something is wrong with the second number (0.14 should not be the same in every row, only the first one) and it should return 3 rows ($r1 = mysql_fetch_assoc($aukciono_istorija_while) has 3 rows in the database). I do not know what is wrong here, using one while loop, everything is just fine. Could someone help me, please?
I have found the solution. I should use only one query in the while loop. Instead of this block of code:
$aukciono_laimetojai_while = mysql_query("SELECT id, user_id, date, win
FROM tb_auction_winners WHERE user_id = 206");
$aukciono_istorija_while = mysql_query("SELECT user_id, aukciono_id,
COALESCE(SUM(bid), 0) AS bid, date FROM tb_aukciono_istorija
WHERE user_id = 206 GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_istorija_while)) {
while ($r2 = mysql_fetch_assoc($aukciono_laimetojai_while)) { ?>
<tr>
<td><?php echo $r2['date']; ?></td>
<td><?php echo $r2['win'] - $r1['bid']; ?> Eur</td>
<td>0 Eur</td>
<td>Plačiau <?php echo $r2['win'] . ' - ' . $r1['bid']; ?></td>
</tr>
<?php } } ?>
I should use this (1 query, 1 while loop):
$aukciono_laimetojai_ir_aukciono_istorija_while = mysql_query("
SELECT tai.aukciono_id tai_aukciono_id, taw.win taw_win, taw.date taw_date,
COALESCE(SUM(tai.bid), 0) tai_bid FROM tb_auction_winners taw
JOIN tb_aukciono_istorija tai ON taw.id = tai.aukciono_id
WHERE tai.user_id = $usid GROUP BY aukciono_id");
while ($r1 = mysql_fetch_assoc($aukciono_laimetojai_ir_aukciono_istorija_while)) { ?>
<tr>
<td><?php echo $r1['taw_date']; ?></td>
<td><?php echo $r1['taw_win'] - $r1['tai_bid']; ?> Eur</td>
<td>0 Eur</td>
<td class="placiau" data-aukciono-id=
"<?php echo $r1['tai_aukciono_id']; ?>">Plačiau</td>
</tr>
<?php } ?>
Hey guys I need your help, I have this table
and I want to shows like this FIDDLE, really I don't know how to do this because sometimes just exist two columns(prov_name ) and sometimes exist more that two rows please help me if you can !
Hope you understand me. Thanks so much !
In this way I can be able to select data from Joomla.
$db =& JFactory::getDBO();
$query = 'SELECT prov_name FROM provprices where CA_id = '.$CA_id;
$db->setQuery($query);
$result = $db->loadObjectList();
$prov_name = $result[0];
echo $prov_name->prov_name;
First off, in order for your data to be presented like that obviously it must be grouped accordingly.
The first row is, the prov_name's, so you can use GROUP BY or you cal also do it in PHP. Based of the sample data, it should have from 1 to 6.
Then the second row is just a simple unitval and totval according to how many prov_name's.
Third is the and the rest is the grouping of the values. See Example:
$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME;charset=utf8', 'USERNAME', 'PASSWORD');
$data = array();
$results = $db->query("SELECT * from YOUR_TABLE_NAME");
while($row = $results->fetch(PDO::FETCH_ASSOC)) {
$data[$row['prov_name']][] = $row;
}
$keys = array_keys($data);
$size = count($keys);
$vals = array();
// grouping:
// if there are six (cam1 to cam6)
// then group them by cam1, ... to cam6, then repeat until theres no more left
while(count($data) > 0) {
foreach($keys as $key) {
if(!empty($data[$key])) {
$vals[] = array_shift($data[$key]);
} else {
unset($data[$key]); // remove them if empty
}
}
}
$vals = array_chunk($vals, $size); // split them by how many prov_names
?>
<table border="1" cellpadding="10">
<!-- PROV NAMES -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<th colspan="2"><?php echo "prov_name $x"; ?></th>
<?php endfor; ?></tr>
<!-- unitval totvals -->
<tr><?php for($x = 1; $x <= $size; $x++): ?>
<td>unitval</td><td>totval</td>
<?php endfor; ?></tr>
<!-- the grouped values -->
<?php foreach($vals as $val): ?>
<tr>
<?php foreach($val as $v): ?>
<td><?php echo $v['unitval']; ?></td>
<td><?php echo $v['totval']; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
I think you must do this first,
try looping to show prov_name in horizontal,
and then you fetch again in query with
"SELECT * FROM table_test where prov_name = '$prov_name'"
in Variable $prov_name you must have a value with CAM2 or CAM3 .etc
sorry just a logic :)
So I have this code setup to display food entries by expiration dates on a table, from top to bottom, earliest to latest. If they are within three days of the current date, the text will be yellow,if it is anytime after that, the text will be red.
Anyway, I recently was testing and realized that if two entries have the same date, the way I have my code setup, it would pull show only one of the entries however number of times that expiration date is on the table (the very first entry with that date added to be exact).
So I setup a for loop uses some queries to check if there were more than one entries and then displayed each one subsequently. Problem is, I get to the final index for the loop, and the Name associated with that date will not show up. Heres the Code:
<?php
$sql1=mysql_query("SELECT email from loggedin WHERE session_id='$userid'");
$sess=mysql_fetch_array($sql1);
$newValue=$sess['email'];
$sqlLength = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue'");
?>
<div class="table-responsive">
<h2 class="sub-header">Expiration Dates</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Food Name</th>
<th>Brand Name</th>
<th>Food Group</th>
<th>Location</th>
<th>Expiration Date</th>
</tr>
</thead>
<tbody>
<?php
$i = 0;
while($test = mysql_fetch_array($sqlLength)){
$dates[$i] = $test['ExpirationDate'];
$i++;
}
$length=count($dates);
for($x=0;$x<$length;$x++){
$year[$x]=substr($dates[$x],6,4);
$month[$x] = substr($dates[$x],3,2);
$day[$x] = substr($dates[$x],0,2);
}
for($x=0;$x<$length-1;$x++){
for($y=$x+1;$y<$length;$y++){
if($year[$x] > $year[$y]){
$temp=$year[$x];
$year[$x]= $year[$y];
$year[$y]=$temp;
$temp=$month[$x];
$month[$x]= $month[$y];
$month[$y]=$temp;
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
elseif($year[$x] == $year[$y]){
if($month[$x] > $month[$y]){
$temp=$month[$x];
$month[$x]= $month[$y];
$month[$y]=$temp;
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
elseif($month[$x] == $month[$y]){
if($day[$x] > $day[$y]){
$temp=$day[$x];
$day[$x] = $day[$y];
$day[$y] = $temp;
}
}
}
}
}
for($x=0;$x<$length;$x++){
$orderedDates[$x]= "$day[$x]-$month[$x]-$year[$x]";
}
$currentYear=date("Y");
$currentMonth=date("m");
$currentDay=date("d");
$multiple="false";
for($x=0;$x<$length;$x++){
$class='';
if($multiple=="false"){
$sql2 = mysql_query("SELECT * FROM food WHERE OwnerEmail='$newValue' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
$rows = mysql_fetch_array($sql2);
if(count($rows) > 7){
$y=1;
$multiple="true";
$count=count($rows)/7;
}
}
else{
$rows = mysql_fetch_array($sql2);
$y++;
//$x++;
}
$year=substr($rows['ExpirationDate'],6,4);
$month=substr($rows['ExpirationDate'],3,2);
$day=substr($rows['ExpirationDate'],0,2);
if($year==$currentYear){
if($month==$currentMonth){
if(($day - $currentDay) <= 3){
$class='expBad';
}
}
}
if($currentYear > $year){
$class='expGross';
}
elseif($currentYear == $year){
if($currentMonth > $month){
$class = 'expGross';
}
elseif($currentMonth == $month){
if($currentDay > $day){
$class='expGross';
}
}
}
?>
<tr>
<td><b><?php echo $rows['Name'] ?></td>
<td><b><?php echo $rows['Brand'] ?></td>
<td><b><?php echo $rows['Type'] ?></td>
<td><b><?php echo $rows['Container'] ?></td>
<td><b><?php echo "<span class=\"$class\">".$rows['ExpirationDate']."</span>" ?></td>
</tr>
<?php
if($y == $count){
$multiple="false";
}
}
/*
$er=mysql_query("SELECT Name FROM food WHERE ExpirationDate='$orderedDates[3]'");
$der = mysql_fetch_array($er);
$mer=$der['Name'];
echo $mer;
*/
?>
</tbody>
</table>
Some info to know, my food table has 7 columns.
orderedDates gives me a correctly ordered array (I know there's a better way to do that, but I was just starting php when I wrote that)
There are 4 food entries.
Here's the problem, when I get to $x = 3, for some reason it does not display any of the column values for that entry.
Here are what the entries should look like in the table.
what s Grains Pantry 12-02-2012
why s Grains Pantry 12-02-2012
who s Grains Pantry 08-04-2014
where s Grains Pantry 10-04-2014
I only get the first 3 entries and then the 4th row is empty.
the other two columns are id and OwnerEmail
It turns out the count() function was returning back a number other than what I thought.
Which is I thought $count would equal 1 for the value that had a unique expiration date.
But instead it was 2, which caused it to run the mysql_fetch_array function one more time than I wanted,making it point at a null value;
instead I used the following code to get the number of food items with same dates:
$sql2 = mysql_query("SELECT Name FROM food WHERE OwnerEmail='$emailname' AND ExpirationDate='$orderedDates[$x]'") or die(mysql_error());
$count2=0;
while($rows2 = mysql_fetch_array($sql2)){
$count2++;
}