Populating a DropDown from SQL database in PHP - php

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>';
?>

Related

Creating an array from other arrays

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
);
}

archive php sql date()

I've started something I don't know how to finish. I've built a table called 'blog'. In the blog table there is a column, date. I use the date() function to retrieve this column:
"1360225336".
I want Archives by year followed by month:
-2013
9.September
.
.
.
3.March
2.February
1.January
-2012
12.December
.
.
.
3.March
2.February
1.January
<?php
$query_o = mysql_query("SELECT * FROM `blog`")or die(mysql_error());
while($row_o = mysql_fetch_assoc($query_o)){
$new_date_y = date("Y", $row_o[date]);
$new_date_m = date("n", $row_o[date]);
$query_h = mysql_query("SELECT * FROM `blog` where date = $new_date_y ")or die(mysql_error());
while($row_h = mysql_fetch_assoc($query_h)){
$new_date_m1 = date("n", $row_o[date]);
echo'<ul>$new_date_y</ul>';
echo'<li>$new_date_m1</il>';
}
}
?>
Try this
$result = mysql_query("select distinct YEAR(date) as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
echo $year."<br/>";
//echo "select * from blog where YEAR(date) = '$year'";
$result1 = mysql_query("select * from blog where YEAR(date) = '$year'") or die(mysql_error());
while($row1 = mysql_fetch_assoc($result1))
$one_year_posts[] = $row1;
foreach($one_year_posts as $post){
//echo $post['date']."<br>";
$months.= date('m F',strtotime($post['date'])).", ";
}
echo $months = rtrim($months," ,");
}
Try this query:
SELECT * FROM `blog` GROUP BY YEAR(`Date`), MONTH(`Date`);
If you also need day, just add , DAY(`Date`) at the end.
$result = mysql_query("select distinct FROM_UNIXTIME(date,'%Y') as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
echo "<br/>-".$year."<br/>";
$result = mysql_query("select * from blog where FROM_UNIXTIME(date,'%Y') = '$year'") or die(mysql_error());
while($row = mysql_fetch_assoc($result))
$one_year_posts[] = $row;
foreach($one_year_posts as $post){
echo strftime('%d %B',$post['date']).", ";
}
}
Note: Use mysqli_ instead of mysql_ functions. The latter is deprecated and no longer maintained.
after trying a lot of new thing finally i succeed
<?php
$result = mysql_query("select distinct FROM_UNIXTIME(date,'%Y') as year from blog ") or die(mysql_error());
$years = array();
while($row = mysql_fetch_assoc($result))
$years[] = $row['year'];
$one_year_posts = array();
foreach($years as $year){
$result1 = mysql_query("select distinct FROM_UNIXTIME(date,'%M') as m from blog where FROM_UNIXTIME(date,'%Y') = '$year'") or die(mysql_error());
$ms = array();
while($row1 = mysql_fetch_assoc($result1))
$ms[] = $row1['m'];
$one_year_posts = array();
foreach($ms as $m){
$date= $year;
$date.= " ";
$date.= $m;
echo <<<PRINT
<li><a href='search.php?archie=$date'>$date</a></li>
PRINT;
}
}
?>

How can I categorise MySQLi result set?

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.

Can't fill ComboBox option

$i = 1;
$tbl = "";
$field = "";
if($_POST['jenis']=="Bahan Baku")
{
$tbl = "bahanbaku";
$field = "bb_";
}
else if($_POST['jenis']=="Bahan Penunjang")
{
$tbl = "bahanpenunjang";
$field = "bp_";
}
<select class="span10" name="jenis" class="add-on">
<?php
$opt = "SELECT bb_nama FROM ".$tbl."";
$result = mysql_query($opt);
if($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}
?>
</select>
The result is none of the selected rows displayed. which line is wrong?
while ($row = mysql_fetch_array($result))
{
echo "<option>". $row[$field.'nama'] ."</option>";
}
Change
if($row = mysql_fetch_array($result))
for
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
in order to iterate over the entire resultset
$opt = "SELECT bb_nama FROM ".$tbl."";
you are selecting the field bb_nama, but when you retrieving time you are doing $row[$field.'nama']. the field is mismatch while the table value is changing.
in other case, query should be
$opt = "SELECT ".$field."_nama FROM ".$tbl."";

Store Column Values in Array

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;
}

Categories