I'm pulling data from a table in two different queries, and I am trying to compare the date field from both results. When the date is equal I want to add the two ending balances from each query together and put the results into a new array. The problem I am having is on line 59, The error I get is
Notice: Undefined offset.
This is what I have:
include "../sqlConnect.php";
mysqli_set_charset($dbScrap, 'utf8');
$dataArray[] = array();
$dateArray[] = array();
$balanceArray[] = array();
//Smaller
$query = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account= '5010-15-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap, $query) or die("SQL Error 1: " .
mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$dateArray[] = array(
'Date' => $Date15
);
$balanceArray[] = array(
'EndingBalance' => $EndingBalance15
);
}
\\Bigger
$query1 = "SELECT Account, Date ,SUM(EndingBalance) AS 'Month_Total' FROM gl_period_posting_history
INNER JOIN gl_account
ON gl_period_posting_history.AccountKey = gl_account.AccountKey
Where gl_account.Account ='5010-08-0000' AND FiscalYear > 2012
GROUP BY Account, Date";
$result = mysqli_query( $dbScrap,$query1) or die("SQL Error 1: " . mysqli_error($dbScrap));
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)){
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
for($i = 0; $i < $dateArray; $i++){
if($Date08 == $dateArray[$i]) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$i];
$Date = $Date08;
}else{
$message = "Date Not Equal";
$Date = $Date08;
$EndingBalance = $EndingBalance08;
}
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date,
'Message' => $message
);
}
echo "<pre>";
print_r($dataArray);
//echo json_encode($dataArray);
echo "</pre>";
$result->close();
/* close connection */
$dbScrap->close();
Thank you for your help.
The problem is in for loop, correction:
for ($i = 0; $i < count($dateArray); $i++){
In the loop where you fetch results from your first query, use the date from each row as the key in $balanceArray, like this:
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$EndingBalance15 = $row['Month_Total'];
$Date15 = $row['Date'];
$balanceArray[$Date15] = $EndingBalance15;
}
Then in the loop where you fetch results from your second query, you can use isset to check if that date exists in the results from the first query, like this.
while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
$Date08 = $row['Date'];
$EndingBalance08 = $row['Month_Total'];
if (isset($balanceArray[$Date08])) {
$message = "Date Equal";
$EndingBalance = $EndingBalance08 + $balanceArray[$Date08];
} else {
$message = "Date Not Equal";
$EndingBalance = $EndingBalance08;
}
$dataArray[] = array(
'EndingBalance' => $EndingBalance,
'Date' => $Date08,
'Message' => $message
);
}
Related
When I view the record from the database, the result is displayed as null.
This is what I tried so far:
<?php
include("db.php");
$fetchqry = "SELECT mid, planid, paid_date, expire_date FROM payment";
$result = mysqli_query($conn, $fetchqry);
$num = mysqli_num_rows($result);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if (strtotime(date("Y/m/d")) < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
}
echo json_encode($row);
?>
Add the values you read from the DB to an array, then encode that.
$output = [];
$today = strtotime(date("Y/m/d"));
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$mid = $row['mid'];
$planid = $row['planid'];
$date1 = $row['expire_date'];
if($today < strtotime($date1))
{
$status = "Active";
}
else
{
$status = "Expired";
}
$output[] = ['mid' => $mid, 'planid' => $planid, 'status' => $status];
}
echo json_encode($output);
I'm attempting to get data from a SQL database in order to populate a couple drop downs. This is an excerpt, but I can post more if you'd like. I didn't include it all because its more than a couple lines.
$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");
$tracker = 0;
$dataArray = array();
$groupsArray = array();
$DateFormat1 = array();
$DateFormat2 = array();
$DayNumber = array();
$Month = array();
$Year = array();
while ($row = mysql_fetch_array($queryData)) {
$dataArray[$tracker] = $row['DateTime'];
$tracker++;
}
$tracker = 0;
while ($row = mysql_fetch_array($queryGroups)) {
$groupsArray[$tracker] = $row['GroupName'];
$tracker++;
}
$tracker = 0;
foreach ($dataArray as $l) {
$p = strtotime($l);
$x = getdate($p);
$DateFormat1[$tracker] = date("D M d, Y", $x);
$DateFormat2[$tracker] = date("M Y", $x);
$DayNumber[$tracker] = date("z", $x);
$Month[$tracker] = date("n", $x);
$Year[$tracker] = date("Y", $x);
$tracker++;
}
echo "<div id='Period1'> <span class='regblue'>Start</span><select name='startdate'><option value=''></option>";
foreach($DateFormat1 as $x)
echo "<option selected value='$x'>$x</option>";
echo "</select> </div>";
For some reason, the drop down remains empty no matter what I try.
Why are you using such a complex code. Use the power of php of integrating itself with HTML.
Try this Style.
And check if you have established a connection with the database or not.
<?php
require_once('connection.php'); //establish the connection with the database on this page.
$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");
$result = mysql_fetch_array(mysql_query($queryData)); //$result now has database tables
$resultGroups = mysql_fetch_array(mysql_query($qrueryGroups)); //$resultGroups has now database tables
?>
<select name='Date'>
<?php
while($row = mysql_fetch_array($result))
{
?>
<option values=<?php echo($row['DateTime']); ?><?php echo($row['DateTime']); ?></option>
<?php
}
?>
</select>
<?php
?>
You may try like this
<?php
require_once('db_connect.php'); //connect with the database.
$queryData = mysql_query("SELECT DISTINCT DateTime AS DateTime FROM 'historicaldata' ORDER BY YEAR(DateTime), DAYOFYEAR(DateTime)");
$queryGroups = mysql_query("SELECT DISTINCT histgroupname AS GroupName FROM 'historicalgroups' WHERE `histgroupID` < 10 ORDER BY `histgroupname`");
$result = mysql_fetch_array(mysql_query($queryData)); //$result now has database tables
$resultGroups = mysql_fetch_array(mysql_query($qrueryGroups)); //$resultGroups has now database tables
echo '<select name="Date" id="Date">';
while($row = mysql_fetch_assoc($result))
{
echo '<option values=' . $row["DateTime"] . '>' . $row["DateTime"] . '</option>';
}
echo '</select>';
?>
Hi im trying to populate an array with indexes like 'user1' and 'score1' to return to a function.
My Code: It comes up with unindentified variable errors
$query = 'select * from users order by score desc';
$result = mysql_query($query) or die (mysql_error());
$highscore = array();
$count = '0';
while($row = mysql_fetch_array($result))
{
$count++;
$highscore = array('user' . $count => $row['username'], 'score' . $count => $row['score']);
}
return $highscore;
You should use numeric values when you want to use them.. You used a string representation of zero ('0')
$count = 0;
$highscore = array();
while($row = mysql_fetch_array($result))
{
$count++;
$highscore['user' . $count] = $row['username'];
$highscore['score' . $count] = $row['score'];
}
If you wanted 'high score' you might want to use max and just return 1 row.
I'm trying to store the title(summary) and date(created) of an event in an array. But I think im missing something in my loop.
<?php
$summary = array();
$date = array();
mysql_connect('mysql.server', 'myUsername', 'myPass') or die('Could not connect: ' . mysql_error());
mysql_select_db("mxgsite") or die(mysql_error());
$query_summary = mysql_query('SELECT summary FROM event_info') or die(mysql_error());
$query_date = mysql_query('SELECT created FROM event_details') or die(mysql_error());
$row_summary = mysql_fetch_array($query_summary);
$row_date = mysql_fetch_array($query_date);
$i = 0;
while(($row1 = mysql_fetch_array($query_summary))) {
$row2 = mysql_fetch_array($query_date);
$summary[] = $row['summary'];
$date[] = $row['created'];
echo $summary[$i] . " " . $date[$i] . "<br ?>";
$i++;
}
I know i'm getting values because I can echo out 1 value, but if I want to put all the values in an array and try to echo out that array I keep getting blank values?
It seems to me like you are trying to do too many things here. Since the 2 sets of values are not being stored in a way where they are related/linked to each other, you might as well deal with them in separate while loops. Try something like this:
while ($row = mysql_fetch_array($query_summary)){
$summary[] = $row[0];
}
while ($row = mysql_fetch_array($query_date)){
$date[] = $row[0];
}
If you want to relate the tables, per the comments above, you can try something more like:
$result = mysql_query('SELECT a.eventid, a.summary, b.created
FROM event_info a
join event_details b
on a.eventid = b.eventid');
$events = array();
while ($row = mysql_fetch_array($result)){
$event = array();
foreach ($row as $key=>$value){
$event[$key]=$value;
}
$events[] = $event;
}
I'm trying to display this query output in a Highchart (line). I’dlike to know how to input this MySQL loop into the Highchart.
<?php
$qu = "SELECT *,COUNT(url) FROM clicks WHERE url='aaaa' GROUP BY date";
$result = mysql_query($qu) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$hits = $row['COUNT(url)'];
$date = $row['date'];
}?>
Maybe this gives you some idea:
$label = array();
while($row = mysql_fetch_array($result))
{
$label[] = $row["date"];
$data_count[] = (float)$row["COUNT(url)"];
}
$series = array();
$series[] = array("name"=> 'total', "color" => "#4572a7", "data" => $data_count);
$data = array();
$data["chart"]["renderTo"] = "report";
$data["chart"]["defaultSeriesType"] = "column";
$data["title"]["text"] = "Some Title Here";
$data["series"] = $series;
$data["xAxis"]["categories"] = $label;
$data["yAxis"]["allowDecimals"] = true;
header('Content-Type: application/json; charset: utf-8;');
echo json_encode($data);