good day I write a function where I want to display the records which will match on the dates last week but it only loop one time. Is there a way to return all the dates and show all the records that will match with the dates? Here is my code.
function fetch_week(){
$today = date('F d Y');
for($i = 1; $i <= 7; $i++)
{
$repeat = strtotime("-1 day",strtotime($today));
$today = date('F d Y',$repeat);
$output = '';
$conn = mysqli_connect("localhost", "root", "", "sample");
$sql = "SELECT * FROM list WHERE datee = '".$today."' ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if(empty(mysqli_num_rows($result))){
echo '<td colspan="6"><h5><center>NO RECORDS.</center></h5></td>';
}
else{
while($row = mysqli_fetch_assoc($result)){
$output .= '<tbody id="appTable">
<tr class="content" style="font-weight: normal text-align: center">
<td>'.$row["name"].'</td>
<td>'.$row["address"].'</td>
<td>'.$row["contact"].'</td>
<td>'.$row["datee"].'</td>
</tr>
</tbody>
';
}
return $output;
}
}
}
You can create an array and return that after your loop ends. Check below code:
function fetch_week() {
$today = date('F d Y');
$response = [];
for ($i = 1; $i <= 7; $i++) {
$repeat = strtotime("-1 day", strtotime($today));
$today = date('F d Y', $repeat);
$output = '';
$conn = mysqli_connect("localhost", "root", "", "sample");
$sql = "SELECT * FROM list WHERE datee = '" . $today . "' ORDER BY id DESC";
$result = mysqli_query($conn, $sql);
if (empty(mysqli_num_rows($result))) {
echo '<td colspan="6"><h5><center>NO RECORDS.</center></h5></td>';
} else {
$output = '<tbody id="appTable">';
while ($row = mysqli_fetch_assoc($result)) {
$output .= '<tr class="content" style="font-weight: normal text-align: center">
<td>' . $row["name"] . '</td>
<td>' . $row["address"] . '</td>
<td>' . $row["contact"] . '</td>
<td>' . $row["datee"] . '</td>
</tr>';
}
$output .= '</tbody>';
$response[] = $output;
}
}
return $response;
}
You need to loop through on output to print your data. But apart from all these, your code is widely open for SQL injection. Please be careful and do changes for that. Also you don't need to create tbody again and again in loop. You can do that first time only which I have changed in above code as well.
Hope it helps you.
Related
Basically I am building a calendar page that displays the months, and the days of the month(pulled from my database) and then any days that are inside the "start_date - end_date" variables are displayed with a different cell background color to the days that don't have a start or end date assigned, I have it working to an extent but it's only displaying the earliest of each months record rather than all the results.
ie.
2015-03-12(start) - 2015-03-16(end)
2015-03-03(start) - 2015-03-10(end)
And rather than display like this...
`1 2 [3 4 5 6 7 8 9 10] 11 [12 13 14 15 16] 17 18 19 20 ...`
it's just showing the [3 - 10] record, here is my current code..
<table width="100%" cellspacing="0">
<?php
$cmonth = date('F');
$cyear = date('Y');
$sql = "SELECT * FROM calendar WHERE year = '$cyear' ORDER BY m_order ASC";
$res = mysql_query($sql);
while ($rows = mysql_fetch_array($res)) {
$month_end = $rows['days_in_month'];
$month_name = $rows['month_name'];
$m_order = $rows['m_order'];
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
$row = mysql_fetch_assoc($res2);
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $days)
{
if(in_array($days, range($start, $end)))
{
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else
echo "<td align=\"center\">" . $days . "</td>";
}
?>
</tr>
<?php } ?>
</table>
Also I am aware of the dangers not using mysqli but I am just learning this on my local machine and plan on researching updated strategies once I get the functions working, so I'll know if my functions are broken or my coding is.
Thanks
try with common function to read data from table
function db_set_recordset($sql) {
$qry = mysql_query($sql);
$row= array();
while($out = mysql_fetch_assoc($qry)) {
$row[] = $out;
}
return $row;
}
$cmonth = date('F');
$cyear = date('Y');
$sql = "SELECT * FROM calendar WHERE year = '$cyear' ORDER BY m_order ASC";
$res = mysql_query($sql);
while ($rows = mysql_fetch_array($res)) {
$month_end = $rows['days_in_month'];
$month_name = $rows['month_name'];
$m_order = $rows['m_order'];
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
$row = db_set_recordset($res2);
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $days)
{
if(in_array($days, range($start, $end)))
{
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else
echo "<td align=\"center\">" . $days . "</td>";
}
?>
</tr>
<?php } ?>
</table>
You are only fetching one row of your result set from your trips table. You need something like this to get the second one.
while ( $row = mysql_fetch_assoc($res2) ) {
/* process each row */
}
You're doing this right for your other result set. Take a look at the examples here: http://php.net/manual/en/function.mysql-fetch-assoc.php
You'll need to run through the result set rows and accumulate your "hot" days, then render the days just once. Something like this:
$sql2 = "SELECT * FROM trips WHERE start_date LIKE '____-0$m_order-__' ORDER BY start_date ASC";
$res2 = mysql_query($sql2);
while ($row = mysql_fetch_assoc($res2)) {
$stdate = $row['start_date'];
$s = date_parse_from_format("Y-m-d", $stdate);
$endate = $row['end_date'];
$e = date_parse_from_format("Y-m-d", $endate);
$start = $s['day'];
$end = $e['day'];
$hot_days = array();
foreach(range(1, $month_end) as $days) {
$hot_days[$days] = 0;
if(in_array($days, range($start, $end))) {
$hot_days[$days] ++;
}
}
}
/* now you have a $hot_days array with nonzero values for interesting days */
?>
<tr>
<td width="80px"><?php echo $month_name; ?></td>
<?php
foreach(range(1, $month_end) as $day) {
if($hot_days[$day] > 0) {
echo "<td style=\"background-color: #ccc;\" align=\"center\">" . $days . " </td>";
}
else {
echo "<td align=\"center\">" . $days . "</td>";
}
}
?>
</tr>
With respect, I don't have time to debug this.
I have a PHP page where its querying data from the database and putting it in a table. The first column is where I would like the user to assign a person to that row. I was able to do that successfully (the select in a loop) but now I'm having a problem when its getting pushed out to the other page.
Below is the first page:
$sql = "SELECT * FROM meetingDump WHERE Meeting_ID IN ($Series)";
$rs=odbc_exec($conn,$sql);
while($row = odbc_fetch_array($rs))
{
$ID = odbc_result($rs,"ID");
$Meeting_ID = odbc_result($rs,"Meeting_ID");
$Title = odbc_result($rs,"Title");
$StartTime = odbc_result($rs,"StartTime");
$EndTime = odbc_result($rs,"EndTime");
$Organizer = odbc_result($rs,"Organizer");
echo '<tr>
<td>';
{
$box1 = array();
$result1 = "SELECT FullName FROM User";
$rs1=odbc_exec($connu,$result1);
while($row = odbc_fetch_array($rs1)) { $box1[] = $row; }
}
/* Generate select box contents */
$AssignedTo = '<select name="AssignedTo[]" onchange="autoSubmit()">';
$AssignedTo .= '<option selected="selected">---< Select Engineer >---</option>';
if (!empty($box1)) {
foreach ($box1 as $k => $v) {
$AssignedTo .= '<option value="'.$v['FullName'].'">'.$v['FullName'].'</option>';
}
}
$AssignedTo .= '</select>';
/* Output */
echo $AssignedTo;
echo '
</td>
<input name="AssignedID[]" type="hidden" value="' . $ID . '" />
<td>' . $Meeting_ID . '</td>
<td>' . $Title . '</td>
<td>' . $StartTime . '</td>
<td>' . $EndTime . '</td>
<td>' . $Organizer . '</td>';
}
Now for the second page I currently have:
foreach($_POST['AssignedTo'] as $AssignedTo)
{
echo '<br>' . $AssignedTo;
}
That gets me all the selected names, which is perfect, but I'm trying to correlate the assignedTo field with the meeting_id field.
Any ideas?
UPDATE:
The comment from AeroX helped me figure it out!
$AssignedID = $_POST['AssignedID'];
$AssignedTo = $_POST['AssignedTo'];
foreach ($AssignedID as $Key => $value)
{
echo $AssignedID[$Key] .' '. $AssignedTo[$Key];
echo '<br>';
}
In your example, because of the way the POST variables $_POST['AssignedID'] and $_POST['AssignedTo'] will be populated you can just pull the Value from each Array where they both have matching Keys. This will then give you the related records.
Something like the below should work for you:
$AssignedID = $_POST['AssignedID'];
$AssignedTo = $_POST['AssignedTo'];
foreach(array_keys($AssignedID) as $Key)
{
echo $AssignedID[$Key];
echo $AssignedTo[$Key];
}
I'm trying to use a row from a MySQLi result within a secondary query.
but im getting some unexpected results.
<?php
$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$rows[] = $row; }
foreach($rows as $row)
{
echo $row['course'] . "<br/>";
$query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time";
$result2 = $mysqli->query($query2);
$today_uk = " " . date("d/m/y");
while($row2 = $result2->fetch_array())
{
$rows2[] = $row2;
}
foreach($rows2 as $row2)
{
$date = $row2['date'];
$date = date("d/m/y", strtotime($date));
echo '<div style= "width:600px; font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;
}
}
$result->close();
$mysqli->close();
?>
my page currently looks like -
ipswich
11:00-running
12:00-flamingo rider
14:00-lightning
norwich
11:00-running
12:00-flamingo rider
14:00-lightning
13:10-ed is back
14:05-redrum
17:05-pickle
whereas I want
ipswich
11:00-running
12:00-flamingo rider
14:00-lightning
norwich
13:10-ed is back
14:05-redrum
17:05-pickle
to be returned.
How can I free the result in the second for each query?
Good heavens what a mess.
Try this.
<?php
$mysqli = new mysqli('connection');
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT course FROM dailytips WHERE date = 20130724 GROUP BY course";
$result = $mysqli->query($query);
$row = $result->fetch_array();
echo $row['course'] . "<br/>";
do {
$query2 = "SELECT horse, time, date FROM dailytips WHERE date = 20130724 and course ='{$row['course']}' ORDER BY time";
$result2 = $mysqli->query($query2);
$today_uk = " " . date("d/m/y");
while($row2 = $result2->fetch_array())
{
$date = $row2['date'];
$date = date("d/m/y", strtotime($date));
echo '<div style= "width:600px;font-family:verdana;"><div style="float:left; width:400px; margin-bottom:10px; margin-top:10px;">'.$row2['time'] . "-" . $row2['horse'] .' </div>' ;
}
} while ( $row = $result->fetch_array() );
$result->close();
$result2->close();
$mysqli->close();
?>
How can I change the code below so that I am only echoing each unique '$course' only once?
eg. currently my results look like =
ipswich-11:00-running
ipswich-12:00-flamingo rider
ipswich-14:00-lightning
norwich-13:10-ed is back
norwich-14:05-redrum
norwich-17:05-pickle
but I would like them to look like =
Ipswich
11:00-running
12:00-flamingo rider
14:00-lightning
norwich
13:10-ed is back
14:05-redrum
17:05-pickle
I thought about doing a mysqli query in a for each loop, but surely there is a better way?
My code =
<?php //connection block
if ($mysqli->connect_error) {die('Connect Error: ' . $mysqli->connect_error);}
$today = date("Ymd");
$query = "SELECT horse, course, time, date FROM dailytips WHERE date = $today ORDER BY course, time";
$result = $mysqli->query($query);
$today_uk = " " . date("d/m/y");
while($row = $result->fetch_array())
{ $rows[] = $row; }
echo "<h2>tips for" .$today_uk. "</h2>";
foreach($rows as $row)
{
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$horse = $row['horse'];
$time = $row['time'];
$course = $row['course'];
echo
'<div style= "width:600px; font-family:verdana;">
<div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">
'.$row['course']. "-" .$row['time'] . "-" . $row['horse'] .'
</div>' ;
}
$result->close();
$mysqli->close();
?>
I guess you could nest a foreeach statement inside another.
so...
$query = "SELECT course, FROM dailytips WHERE date = $today ORDER BY course, time";
while($row = $result->fetch_array())
{ $rows[] = $row; }
echo "<h2>tips for" .$today_uk. "</h2>";
foreach($rows as $row) {
$query = "SELECT horse, time, date FROM dailytips WHERE course = $row['course'];
while($row = $result->fetch_array())
{ $rowDetail[] = $rowDetails; }
foreach($rows as $row) {
$date = $row['date'];
$date = date("d/m/y", strtotime($date));
$horse = $row['horse'];
$time = $row['time'];
$course = $row['course'];
echo
'<div style= "width:600px; font-family:verdana;">
<div style="float:left; width:400px; margin-bottom:10px; margin-top10px;">
'.$row['course']. "-" .$row['time'] . "-" . $row['horse'] .'
</div>' ;
}
}
I would consider putting your results in a table opposed to DIVs as this would seem to fit what you are trying to do much better. Set the table up before the loop and close once the loop has been exited.
I am attempting to sort results using ORDER BY DESC, but results are being sorted by foreach values:
while($row = mysqli_fetch_array($sqlgroup)){
$member_array = $row["member_array"];
if ($member_array !=""){
$memberArray = explode(",", $member_array);
$i = 0;
$cashstatsList .= '
';
foreach($memberArray as $gkey => $mvalue){
$i++;
$arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg,
SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg
FROM player, groupcash
WHERE (player.id = grpcsh_plrid) AND (player.id = $mvalue) AND (groupcash.grpcsh_groupid = $groupid)
AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC
";
$arraystatsResutls = mysqli_query($link, $arraystats);
if (!$arraystatsResutls){
$cashstatsList .= '
<tr>
<td>
No results available for listed dates
</td>
</tr>';
} else {
while($row = mysqli_fetch_array($arraystatsResutls)){
$memberFirstName = $row["first_name"];
$memberLastName = $row["last_name"];
$sum = $row["memsum"];
$avg = $row["memavg"];
$win = $row["memcntpos"];
$loss = $row["memcntneg"];
if ($memberFirstName == "" || $memberLastName == ""){
$sqlName = mysqli_query($link, "SELECT first_name, last_name FROM player WHERE id='$mvalue' LIMIT 1") or die ("Sorry we had a mysql error!");
while ($row = mysqli_fetch_array($sqlName)) {
$memberFirstName = $row["first_name"]; $memberLastName = $row["last_name"];
}
}
if ($sum == ""){
$sum = "0";
}
if ($avg == ""){
$avg = "0";
}
if ($win == ""){
$win = "0";
}
if ($loss == ""){
$loss = "0";
}
$cashstatsList .= '
<tr align="center">
<td>
' . $i . '
</td>
<td>
' . $memberFirstName . ' ' . $memberLastName . '
</td>
<td>
$' . $sum . '
</td>
<td>
$' . $avg . '
</td>
<td>
' . $win . '/' . $loss . '
</td>
</tr>';
}
}
}
}
}
You have to select all member of the group at once in the query so they can be "ORDER BY", here you have only results on the same member for each request so the order doesn't change anything in the order of result between member.
If $member_array is like 1,454,33,22 you can just remove the foreach and use the query :
$arraystats = "SELECT player.first_name, player.last_name, SUM(groupcash.grpcsh_earnings) AS memsum, AVG (groupcash.grpcsh_earnings) AS memavg,
SUM(groupcash.grpcsh_w) AS memcntpos, SUM(groupcash.grpcsh_l) AS memcntneg
FROM player, groupcash
WHERE (player.id = grpcsh_plrid) AND (player.id IN ($member_array)) AND (groupcash.grpcsh_groupid = $groupid)
AND (grpcsh_date >= '$thisyr') AND (grpcsh_date <= '$today') ORDER BY SUM(groupcash.grpcsh_earnings) DESC
";
So you will get all the member of the group at the same time and the ORDER BY will work.