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;
}
}
?>
Related
I have this code:
<?php
include 'config.php';
date_default_timezone_set('America/Los_Angeles');
$d = date('Y-m-d');
$m = date("m");
$day = date("d");
$t = date("His");
$ip = $_SERVER['REMOTE_ADDR'];
$c = file_get_contents('http://api.wipmania.com/'.$ip);
echo "<h2>ALL RESULTS TODAY:</h2><table>";
$_GET['c'] = $c;
$sc = $_GET['sc'];
if($c === "key"){
if($sc === "t"){
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
while($row = mysqli_fetch_array($result))
{echo "<tr><td>".$row['key'] . "</td><td> " . $row['country']."</td><td>".$row['ip']."</td></tr>"; }
}
}
echo '</table>';
?>
i have tried without $con: mysqli_fetch_array($result), but it was the same...
But nothings appear...
no error no results...
Please help... Thanks!
You have not connected to the database or queried your results:
$conn = mysqli_connect($hostname,$username,$password,$dbname) or die(mysqli_error());
//...
$your_query = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
$result = mysqli_query($conn, $your_query);
while ($row = mysqli_fetch_array($result)){
//...
}
you forgot mysqli_query.
replace this
$result = "SELECT * FROM main WHERE date = '$d' ORDER BY time";
by
$result =mysqli_query("SELECT * FROM main WHERE date = '$d' ORDER BY time");
You forgot to perform the query.
$result = mysqli_query($con, "SELECT * FROM main WHERE date = '$d' ORDER BY time");
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();
?>
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>';
?>
I would like to know how can I count the results in case I have two loop ! :
$select = "SELECT X FROM Y"
$result = mysql_query($select,$link);
$total = mysql_num_rows($result);
while($row = mysql_fetch_array($result)) {
$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
while($row2 = mysql_fetch_array($result2)) { echo $row2['id']} }
I would like to count how many id I have at the end . Thanks for your help
just give this
$select = "SELECT X FROM Y"
$result = mysql_query($select,$link);
$total = mysql_num_rows($result);
$finalTotal=0;
while($row = mysql_fetch_array($result)) {
$Name = $row['X'];
$select2 = "SELECT id FROM `".$Name."` ";
$result2 = mysql_query($select2,$link) or die ('Erreur : '.mysql_error() );
$total2 = mysql_num_rows($result2);
$finalTotal = $finalTotal + $total2;
while($row2 = mysql_fetch_array($result2)) { echo $row2['id'];} }
echo "total id is ".$finalTotal;
$finalTotal will show you total number of id's
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;
}