I have put together the following code, the problem is that each while loop is only returning one set of data.
$result = mysql_query("SELECT date FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' GROUP BY date");
$i = 1;
echo "<table cellspacing=\"10\" style='border: 1px dotted' width=\"300\" bgcolor=\"#eeeeee\">";
while ($row = mysql_fetch_assoc($result))
{
$date=date("F j Y", $row['date']);
echo $date;
echo "
<tr>
<td>Fixture $i - Deadline on $date</td>
</tr>
";
$result = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result))
{
extract ($row);
echo "
<tr>
<td>$home_user - $home_team V $away_user - $away_team</td>
</tr>
";
}
$i++;
}
echo "</table>";
I should get many dates, and then each set of fixture below.
At the moment, the first row from the first while loop is present, along with the data from the second while loop.
However, it doesn't continue?
Any ideas where I can correct this?
Thanks
Replace
$result = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result))
with
$result1 = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row = mysql_fetch_assoc($result1))`
What happens with your current code is that after executing internal while, next call (in external loop)to mysql_fetch_assoc($result) always returns false (because you just iterated through it in internal loop). All you need is to use a different variable in internal loop.
You're overwriting the $result variable. Change the second one to $result2 or something and see what happens. Make sure you do it when you set the variable, and use it in the query.
You are changing your $result variable in your loop so it is at the end after the inner while loop has run.
you just mixing variables. second result and row should be named differently.
though it would be better to make it with one single query
Well you've got the answer to your question.
I just want to add that usually you can write a smarter SQL query and manage without an inner query with it's loop. And your code will work a lot faster if you'll have 1 query instead of N+1.
LEFT JOIN usually can be used to replace inner looping. Example:
SELECT DISTINCT A.date, B.* FROM table_fixtures A
LEFT JOIN table_fixtures B ON A.date = B.date
WHERE B.compname = "a value"
However this time it looks illogical. I think what you actually do is achievable with a simple query like this:
SELECT * FROM table ORDER WHERE compname="something" ORDER BY date
You are reusing the same variables over for your inner loop and its breaking the outer loop.
Change your inner loop to something like:
$result2 = mysql_query("SELECT * FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' AND date = '$row[date]' ORDER BY date");
while ($row2 = mysql_fetch_assoc($result2))
{
extract ($row2);
echo "
<tr>
<td>$home_user - $home_team V $away_user - $away_team</td>
</tr>
";
}
On a different note - why do you have a GROUP BY date in the first row, when all you have in the projection list is the date?
Related
I have a MySQL, PHP code as follows.
$sql = "SELECT * FROM shipschedule WHERE ship_date BETWEEN '2016-08-01' AND '2016-8-31'";
$result = $mysqli->query($sql);
$e = array();
while($r = $result->fetch_array()) {
$rows = array();
$rows['title'] = $r['title'];
$rows['start'] = $r['ship_date'];
array_push($e, $rows);
}
echo json_encode($e);
The above php code echos
[{"title":"111","start":"2016-08-10"},
{"title":"111","start":"2016-08-10"},
{"title":"111","start":"2016-08-10"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
My question is how I can echo the above as follow instead. Please see that duplicate start dates will be removed by title.
[{"title":"111","start":"2016-08-10"},
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
title 111 has the same 3 start dates, and I need to display it like
{"title":"111","start":"2016-08-10"},
title 222 has the same 2 start dates, and I need to display it like
{"title":"222","start":"2016-08-17"},
{"title":"222","start":"2016-08-16"}]
You could prevent receiving duplicates, and reduce requesting unnecessary data by adjusting your query.
SELECT DISTINCT title, start FROM ...
It would be much easier (and probably faster too) to just get the right (unique) data from MySQL. This can be achieved with the distinct modifier:
SELECT DISTINCT title, start
FROM shipschedule
WHERE ship_date BETWEEN '2016-08-01' AND '2016-8-31'
I'm trying to do nested mysql query but I'm failing
it's as simple as I have a table called 'todo' which is for saving to-do list
when I do
well this code is just to show how I wanna do it logically , I know it doesn't work
and I think it probably needs JOIN or UNION but I couldn't do it
$result = mysqli_query($con,"SELECT type FROM todo WHERE user = '$username' GROUP BY type");
while($row = mysqli_fetch_array($result))
{
echo '<td>- '." ".$row['type'].'</td>';
echo "</br>";
$result2 = mysqli_query($con,"SELECT titleFROM todo WHERE type= '$row['type']'");
while($row = mysqli_fetch_array($result2))
{
echo '<td>-- '." ".$row['title'].'
}
</td>';
echo "</br>";
}
I want the result to like
-Work
--Mr Jack
--Company
-School
--Android Class
--Entrepreneurship
--php development
and so on...
Your inner loop overwrites the outerloop's $row variable. Try using a different variable name in the inner loop. Also, there's an error in your inner loop's SQL query (missing space between the tablename and FROM keyword).
I have a problem today. Where I was displaying the value of "fldFrom" in my php form from database. And I can successfully display it. But my problem is, how can I display only once even there is a same word in "fldFrom". Like, in my fldFrom there are same value of "06/24/2013" so then when I test it, the result is "06/24/2013 06/24/2013" because in my database of "fldFrom" has a two value but different row.. I want them to be combine as one.
Here's my code:
<?php
$all = mysql_query("SELECT fldFrom FROM tbldata WHERE fldWeek = '$get_week'");
while ($row = mysql_fetch_array($all))
{
echo "<input type='text' name='play[]' value='" . $row['fldFrom']."'>";
}
?>
Here's my database:
*I want to display the result of fldFrom in just once only...Not twice like this one
thanks
you can use distinct in SELECT statment like this:
$all = mysql_query("SELECT DISTINCT fldFrom FROM tbldata WHERE fldWeek = '$get_week'");
for more information check it here http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
Edit1:
you can do same with group by, like this:
$all = mysql_query("SELECT fldFrom FROM tbldata WHERE fldWeek = '$get_week' GROUP BY fldFrom");
Just use the DISTINCT operator of SQL:
SELECT DISTINCT fldFrom FROM tbldata WHERE fldWeek = '$get_week'
DISTINCT removes duplicates for you.
For example I have a mysql query that gets some data. Then runs another query based some of the data that it got.
If i just return the first query, in my case $qOne. Everything works great.
BUT, after using my while loop while ($row = mysql_fetch_array($qOne)) It then returns as an empty array. (but the second query I return DOES work)
I tried to see if I could "save" the first query in another var im not messing with like this $savedResult = $qOne, then i'd just return the $savedResult but that did not work.
Does anyone know how I can get my function below to return both of the results? Thanks!
function getFoods($sort, $start, $limit) {
$qOne = mysql_query("SELECT a.id, a.name, a.type, AVG(b.r) AS fra, COUNT(b.id) as tvotes FROM `foods` a LEFT JOIN `foods_ratings` b ON a.id = b.id GROUP BY a.id ORDER BY fra DESC, tvotes DESC LIMIT $start, $limit;");
$i = 0;
$qry = "";
while ($row = mysql_fetch_array($qOne)) {
$fid = $row['id'];
if ($i > 0)
$qry .= " UNION ";
$i++;
$qry .= "SELECT fid, ing, amount FROM foods_ing WHERE fid='$fid'";
}
$qTwo = mysql_query($qry);
return array($qOne, $qTwo);
}
When you have returned the two query result resources, remember that you will need to fetch from them when you actually want to use them. (we don't see the code where you implement that).
To make use of $qOne after you already have looped through it, you must rewind it back to the first position. That is done with mysql_data_seek()
mysql_data_seek($qOne, 0);
All,
I have the following code:
$fetch = mysql_query("SELECT * FROM calendar_events where event_status='booked' and event_type='wedding' GROUP BY start");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['title'] = $row['title'];
$row_array['start'] = $row['start'];
$row_array['end'] = $row['end'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
I'm using the fullcalendar and trying to determine how many events have the same start date. So for example if I had the following dates:
02/06/2012
02/06/2012
03/01/2012
04/05/2012
On dates that have entries in my database I'd like to basically see how many events are on that date and basically say how many spots are left. So for any given date I can have two scheduled events so for 02/06/2012 I'd like it to return "Booked" and for 03/01/2012 I'd like "1 spot left" and etc.
How can I go about doing something like this?
Thanks!
SELECT
start, count(*) as used, 2-count(*) as free
FROM calendar_events
WHERE event_status='booked'
and event_type='wedding'
GROUP BY start
or as by your comment:
SELECT
date(start) as eventdate, count(*) as used, 2-count(*) as free
FROM calendar_events
WHERE event_status='booked'
and event_type='wedding'
GROUP BY date(start)
You're almost there, but you want to use COUNT(*) to return the number of entries in the group. (Remember to give the column an alias so that you can retrieve it.) You should also limit yourself the retrieving only the start column since retrieving columns that aren't in the GROUP BY clause is non-standard.
You're almost done, as the others beat me to the punch. You can try something like this. The important part is simply having the count as mentioned.
$fetch = mysql_query("SELECT id,title,start,end,count(*) as filled FROM calendar_events where event_status='booked' and event_type='wedding' GROUP BY start");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['id'] = $row['id'];
$row_array['title'] = $row['title'];
$row_array['start'] = $row['start'];
$row_array['end'] = $row['end'];
$row_array['filled'] = $row['filled'];
$row_array['booked'] = ($row['filled'] >= 2) ? true : false;
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);