I am trying to create a php script that gets values from a MySQL database, displays a header with the time_stamp that comes from an array, and a sub header with a value called "working_ra" with values underneath those categories. The values that go underneath both categories are being displayed in a table. I am sorting the values of the working_ra in the query so I can run a while loop saying that if they're not the same to display the next working_ra and continue echoing values. Here is my code:
foreach ($date as $i) {
echo "<pre>"."<h2>".$i."</h2>"."</pre>";
$result = mysqli_query ($con, "SELECT * FROM Signing WHERE value1 = 'Ings' AND time_stamp = '$i' ORDER BY working_ra");
$num_results = mysqli_num_rows($result);
$row = mysqli_fetch_array($result);
$desk = $row["working_ra"];
echo "var_desk starting value ","<b>", $desk,"</b>";
echo "<table border='1'>";
echo "<tr>";
echo "<td>", $row["first"],"</td>";
echo "</tr>";
while ($row = mysqli_fetch_array($result)) {
if ((string)$desk != (string)$row["working_ra"]) {
echo "</table>";
$desk = $row["working_ra"];
echo "var_desk next value ","<b>",$desk,"</b>";
echo "<br />";
echo "<table border='1'>";
}
echo "<tr>";
echo "<td>", $row["first"],"</td>";
echo "</tr>";
}
$result->free();
}
$con->close();
Everything works fine until it hits to the second "desk" name. Then the tables that are supposed to be with the next value get shifted to the next date. Here is what it looks like on the page:
aSFashagh, Jeremy, Jeremy, and Johnny are all supposed to be under (Phil, January 10 2013) but is shifted down into the next date and working_ra (Phil, January 20 2013). zffaA, andsdfdsggsdhj are all supposed to be under (Phil,January 20 2013) but are under (Phil, February 25 2013) instead. Whats weird is that the method did work WITHOUT trying to put all of the values in tables.
My assumption is that it could be the ordering of which the table tags are ending and starting, but I have tried numerous amounts of things and still cannot figure out what is wrong.
EDIT
Sorry its very confusing to say. But im trying to get it to where everything is shifted up the way its supposed to be. Where the values of "first" belong with their corresponding time_stamp and working_ra. For instance, the value of Johnny in the MySQL db has a value of working_ra - Phil and a value of time_stamp - January 10, 2013. However as you can see, Johnny is not underneath (Phil, January 10 2013) instead January 20, 2013. Its not the query thats the problem, it has something to do with the while loop and the table and I just can't figure it out.
The only two values that are correct are Zack and Why which have corresponding values of (working_ra = "Bob", time_stamp = "January 10, 2013")
I believe your problem is with the mysql row pointer. You already used mysqli_fetch_array once before the while loop therefore shifting the current mysql row to the next row.
This code
mysqli_data_seek($result, 0);
will reset the pointer.
Place it right before your while loop.
Figured out what the problem was. I needed to add a "/table" tag when the while loop breaks.
Related
I've an associative array. In that array one of the array element is timestamp. I'm logging data from a device for each every minute of a day. I need to display the data in a table. First row of table show start date with start time and start date with end time. Second row of table show next day of data with start time and end time.. and so on. Yes table look like a trip sheet. I'm getting the data from an API. If i pass an date and time in the API, i will get the data for the respective timestamp. For example if pass $srtttime= '2016-05-03 00:00:00' $edtime= '2016-05-04 23:59:59' to the API i will get the data for the date 2016-05-03 and time between '00:00:00' to '23:59:59'. I able to create a row for one date. Below code display a row of record, here i takes the first and last record. See the below image.
enter image description here
$i=0;
$len=0;
$len=count($json_output);
echo "<center><table border='1'></center>";
echo '<tr><th>Date</th><th>Start Time</th><th>Origin Address</th><th>End Time</th><th>Destination Address</th></tr>';
foreach($json_output as $value)
{
if($i==0)
{
$STR_TIME=$value['rtctime'];
$DTE=substr($STR_TIME,0,10);
$STR_TIME=substr($STR_TIME,11,8);
$LAT_S=$value['latitude'];
$LON_S=$value['longitude'];
$ST_ADDR=getaddress($LAT_S,$LON_S);
}
elseif($i==$len-1)
{
$EN_TIME=$value['rtctime'];
$EN_TIME=substr($EN_TIME,11,8);
$LAT_E=$value['latitude'];
$LON_E=$value['longitude'];
$EN_ADDR=getaddress($LAT_E,$LON_E);
}
$i++;
}
echo "<tr>";
echo "<td>$DTE</td>";
echo "<td>$STR_TIME</td>";
echo "<td>$ST_ADDR</td>";
echo "<td>$EN_TIME</td>";
echo "<td>$EN_ADDR</td>";
echo "</tr>";
echo "</table>";
The problem i don't know how to show rows if i pass more than one days timestamp to API i.e. $strttime= '2016-05-03 00:00:00' $edtime= '2016-05-07 23:59:59' here i passing 4 days between start day 03-05-2016 to end day 07-05-2016.Here i have to show four rows. I have tried a lot, but i cannot get it.
First of all you don't need the foreach loop if you only want the first and last records. Your code should be
// Take out first element of array
$str_time = array_shift($json_output);
// Take last element of array
$end_time = array_pop($json_output);
$STR_TIME=$str_time['rtctime'];
$DTE=substr($STR_TIME,0,10);
$STR_TIME=substr($STR_TIME,11,8);
$LAT_S=$str_time['latitude'];
$LON_S=$str_time['longitude'];
$ST_ADDR=getaddress($LAT_S,$LON_S);
$EN_TIME=$end_time['rtctime'];
$EN_TIME=substr($EN_TIME,11,8);
$LAT_E=$end_time['latitude'];
$LON_E=$end_time['longitude'];
$EN_ADDR=getaddress($LAT_E,$LON_E);
echo "<center><table border='1'>";
echo '<tr><th>Date</th><th>Start Time</th><th>Origin Address</th><th>End Time</th><th>Destination Address</th></tr>';
echo "<tr>";
echo "<td>$DTE</td>";
echo "<td>$STR_TIME</td>";
echo "<td>$ST_ADDR</td>";
echo "<td>$EN_TIME</td>";
echo "<td>$EN_ADDR</td>";
echo "</tr>";
echo "</table>";
echo "</center>";
You also had an html markup error with the center end tag.
Can you all please do me a favor ? I am stuck at one place. I want to display all records from a particular date in loop.
for eg. I have 8 records of 29 march, then I want to display header of 29 march and show all the records of 29 march via loop inside a div.
I have 12 records of 29 march and 10 records of 27 march. So far I have been successful in showing 27 march and 29 march dates from a loop via GROUP BY clause but all the records are not showing inside a particular date
Instead of showing all 12 records of 29 march, I am getting only 1 row for each date. I am attaching screenshots and codes for better understanding. What I am getting is below:
As you can see above, I am getting only 1 row for each date instead of all rows related to that date
What I want is below:
Not good editing haha...But I hope it gives you all clear idea as what I want to accomplish. All rows of 29 march via loop and all rows of 27 march via loop. I have attached codes...Please see what can be done
Code (PHP & MYSQLI)
<?php
$Prepare_Dealers_Purchases_Query = "SELECT * FROM `purchases` WHERE `DealerEmailAddress` = '$ShowDealerEmailAddress' GROUP BY `PurchaseDate` ORDER BY `InvoiceID` DESC";
$Fire_Dealers_Purchases_Query = mysqli_query($CreateConnection, $Prepare_Dealers_Purchases_Query);
if(mysqli_num_rows($Fire_Dealers_Purchases_Query) == 0) {
echo "<div id='userFeedbackPanel' style='margin:15% auto;padding:10px;'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td>You have not purchased any product yet</td></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr></tr><tr><td><a onClick=\"$.fn.openProductSelectionBox();\" class=\"StartShoppingButton\"><i class=\"fa fa-shopping-cart fa-lg\"></i> Start Shopping</a></td></tr></table></div>";
} else {
while($Show_Purchases = mysqli_fetch_array($Fire_Dealers_Purchases_Query, MYSQLI_ASSOC)) {
if($Show_Purchases['SelectedProduct'] == "Regular Colours") {
$Shade_Colour = $Show_Purchases['SelectedShade'];
$Purchase_Date = $Show_Purchases['PurchaseDate'];
$Prepare_Dealers_Purchases_As_Per_Date_Query = "SELECT * FROM `purchases` WHERE `DealerEmailAddress` = '$ShowDealerEmailAddress' AND `PurchaseDate` = '$Purchase_Date' ORDER BY `InvoiceID` DESC";
$Fire_Dealers_Purchases_As_Per_Date_Query = mysqli_query($CreateConnection, $Prepare_Dealers_Purchases_As_Per_Date_Query);
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br>";
while($Show_Purchases_As_Per_Date = mysqli_fetch_array($Fire_Dealers_Purchases_As_Per_Date_Query, MYSQLI_ASSOC)) {
echo "<div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td style='background:$Shade_Colour;border-radius:5px;width:54px;height:64px;'></td><td>You purchased ".$Show_Purchases_As_Per_Date['SelectedQuantity']." boxes of ".$Show_Purchases_As_Per_Date['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases_As_Per_Date['FinalAmount']." by using ".$Show_Purchases_As_Per_Date['PaymentMethod']." of ".$Show_Purchases_As_Per_Date['BankName']." at ".$Show_Purchases_As_Per_Date['PurchaseTime']." on ".$Show_Purchases_As_Per_Date['PurchaseDate']."</td></tr></table></div><br>";
}
} else {
$Product_Image = $Show_Purchases['SelectedProductImage'];
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br><div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td><img src='$Product_Image' width='54' height='64'></td><td>You purchased ".$Show_Purchases['SelectedQuantity']." boxes of ".$Show_Purchases['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases['FinalAmount']." by using ".$Show_Purchases['PaymentMethod']." of ".$Show_Purchases['BankName']." at ".$Show_Purchases['PurchaseTime']." on ".$Show_Purchases['PurchaseDate']."</td></tr></table></div><br>";
}
}
}
?>
I am using 2 while loops. 1 loop for getting all dates and other loop for getting rows of that particular date. Please show me where I am making mistake. Thank you all...
It seems you're going there:
else {
$Product_Image = $Show_Purchases['SelectedProductImage'];
echo "<div align='left'>".$Show_Purchases['PurchaseDate']."</div><br><div id='userFeedbackPanel'><table id='feedbackDataStyling' cellspacing='5' cellpadding='5'><tr><td><img src='$Product_Image' width='54' height='64'></td><td>You purchased ".$Show_Purchases['SelectedQuantity']." boxes of ".$Show_Purchases['SelectedProduct']." worth <i class='fa fa-inr'></i> ".$Show_Purchases['FinalAmount']." by using ".$Show_Purchases['PaymentMethod']." of ".$Show_Purchases['BankName']." at ".$Show_Purchases['PurchaseTime']." on ".$Show_Purchases['PurchaseDate']."</td></tr></table></div><br>";
}
There is no while loop here, so only one line is displayed. Add the same while loop as you did in the if statement and it should be fine :)
I've written a PHP script that can populate a table in a particular way so that multiple events (or no events) can be put in one square in an HTML - similar to the layout a calendar would have. But, there's a problem, the while statement I created to fill in squares in the table when there is no data doesn't detect when there is data, and fills the entire table with empty squares. This is what the output looks like (The page is styled using Bootstrap 3). From the mysql data I have provided, these events should be in the square at {Period 1, Monday}.
Here is my data in a mysql database; mysql data
Here is a snippet of the part of the page related to this table;
<?php
$query = "SELECT * FROM configtimetabletwo WHERE term = ".$term." AND week = ".$week." ORDER BY period, day LIMIT 100;";
$results = mysqli_query($conn, $query);
$pp=1; //The current y value of the table
$pd=0; //The current x value of the table
echo '<tr><td>';
while($row = mysqli_fetch_row($results)) {
while((pd!=$row[3] or $pp!=$row[4]) and $pp<6){ //This while statement fills in empty squares and numbers each row.
if($pd==0) {
echo $pp."</td><td>";
$pd++;
}
elseif($pd<5){
echo "</td><td>";
$pd++;
}
else {
echo "</td></tr><tr><td>";
$pd=0;
$pp++;
}
}
echo '<a href="?edit='.$row[0].'" class="label label-default">';
echo $row[5].' '.$row[6].' - '.$row[7]."</a><br>";
}
echo "</td></tr></table>"
?>
I haven't been able to figure out why this happens so far, thanks in advance to anyone who has any idea what's going on.
In the comments below my question, pavlovich pointed out the error. In this case, it was simply an issue of forgetting to use a $ to reference a variable. It would seem that this doesn't throw an error in a while statement like it would elsewhere.
I am making a page where people can make posts. All of those posts are then shown in a table of 24 cells. I can have the last 24 posts shown with no problem, but now I don't know how to show the prior group(s) of posts. How can I fix my code to do that? I actually have this:
(I'm removing lines to make it easy to read)
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC";
// ---check everything is fine---- //
function retrieve_info($result)
{
if($row = mysql_fetch_assoc($result))
{echo $topic_if; echo $topic_subject; //and what I want in every cell
}
}
<table width="100%" height="751" >
<tr><td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td>
<td><?php retrieve_info($result);?></td></tr>
<!-- repeat a few more times :-) -->
</table>
I though that by changing the variable $row with a number before the if statement would alter the output, but I still see the same data printed on screen. What should I do to be able to show next group of posts?
Thanks!!!
At some point when you have hundreds or thousands of records, you are going to want to paginate the results and not just select all records from the table.
To do this you will run one query per 24 records, your sql would be more like this:
$sql = "SELECT
topics.topic_id,
topics.topic_subject
ORDER BY
topics.topic_id DESC
LIMIT 0, 24
";
and for the next 24,
LIMIT 24, 24
then
LIMIT 48, 24
and so on.
You would then make next/previous buttons to click which would refresh the page and dispay the next 24, or you would get the next results with an AJAX request and append the next 24 through the DOM.
This suggests having to take a slightly different approach then calling the same function from each table cell.
More like get the relevant 24 results based on the page number you are on, then loop through the results array and print out the table code with values inside it. Based on if the iterator of the loop is divisible by 4 (looks like your grid is 4x6), you print out new tags for the new row, and that sort of thing.
Search around a bit for pagination in php and mysql to get a sense of how this all fits together.
function retrieve_info($result)
{
while($row = mysql_fetch_assoc($result))
{
$topic_id = htmlspecialchars($row['topic_id']);
$topic_subject = htmlspecialchars($row['topic_subject']);
echo '<td>';
echo $topic_if;
echo $topic_subject; //and what I want in every cell
echo '</td>';
}
}
with my current query and loop:
$sched = mysql_query("SELECT *
FROM `shows`
ORDER BY `shows`.`show_time` ASC")
or die(mysql_error());
echo "<ul>";
while($row = mysql_fetch_array($sched)){
echo "<li><a href=\"#$row[id]\">";
echo $row['title'];
echo "</li>";
}
echo "</ul>";
This works great for displaying my results like this:
Name of show 1
Name of show 2
Name of show 3
However, I want to add an item to the list at the beginning of every change in day so it would display as follows:
Monday
Name of show 1
Name of show 2
Tuesday
Name of show 3
Wednesday
Name of show 4
I can't quite wrap my brain around the loop needed to do this. It might be helpful to know that the field 'show_time' is a datetime type, so it has the information for both time and day of week.
Thanks.
Simple tweak:
echo "<ul>";
$curDay='';
while($row = mysql_fetch_array($sched)){
$d=date('l',strtotime($row['show_time']));
if($d!=$curDay){
echo '<li>'.$d.'</li>';
}
$curDay=$d;
echo '<li><a href="#',$row['id'],'">',$row['title'],"</li>";
}
echo "</ul>";
Initialize $curDay, and then each time through the loop, check to see if the particular day is different than the last time through the loop (or different from the initial value)
The best way to do this is to keep a flag in your loop, and compare to the previous value.
Eg.
$previousDay = '';
while($row = mysql_fetch_assoc()) {
if ($previousDay != date('l', $row['show_time'])) {
echo '<h2>' . $date('l', $row['show_time']) . '</h2>';
}
...
$previousDay = date('l', $row['show_time']);
}
Adjust your query to sort by show_time first.
"SELECT * FROM `shows` ORDER BY `show_time`, `shows` ASC"
Then keep track of the current day as Shad suggests, parsing show_time to determine the day.