Hi ALL,
I’d like to retrieve the last value from an array which is not a tricky thing but I can’t get this to work with a mysql_fetch_array… I’ve tried this :
while ($q_row = mysql_fetch_array($q_inter)) {
echo end($q_row['date']);
} // end while
with this I get a warning message :
Warning: end() [function.end]: Passed variable is not an array or object in /home/path_to_file/members_interaction.php on line 47
Can someone give a guide here? Thanks in advance!
Right.
How many ‘dates’ do you actually need? You’re jumping from 1 to 2, and now 3.
Do you need all 3 but only want to know how to access the last one, or do you just need the 3 one?
You just need to edit your LIMIT to obtain the second result only.
SELECT
date
FROM
interview_questions
WHERE
asker_id = 1
AND
answerer_id = 4
ORDER BY
id DESC
LIMIT 1, 1
Shaydez
September 28, 2010, 5:52pm
4
so you want the second to last one? you can try removing the last array array_pop and then do end()
dunno the past 2 weeks i been having a love and hate relationship with arrays lol
What I wanted is to select i.e. that last 10 rows (dates) from the DB table and then to pick the last one of those. Maybe it’s not the elegant way but I managed to do it with these lines :
$q_inter = mysql_query("SELECT date FROM interview_questions WHERE asker_id = '".$_SESSION['user_id']."' AND answerer_id = '$contact_to_add' ORDER BY id DESC LIMIT 10");
$nr_of_int = mysql_num_rows($q_inter);
if ($nr_of_int > 0) {
for ($i=0; $i<$nr_of_int; $i++) {
$row = mysql_fetch_array($q_inter);
$var[ ] = $row;
}
$last_int_date = $var[$nr_of_int-1]['date'];
Yes, that would be easier, but I need to select the 2 last rows and then pick the first of those 2… i hope it’s clear enough…
That was the 1st thing I tried, but that’s not what I want… It echos all the date values in case of the query :
SELECT date FROM interview_questions WHERE asker_id = '".$_SESSION['user_id']."' AND answerer_id = '$contact_to_add' ORDER BY id DESC LIMIT 2
Anyway, thanks for the tip…
Immerse
September 28, 2010, 4:52pm
8
Try this instead:
echo end($q_row);
Is that what you’re after?
K89
September 28, 2010, 4:56pm
9
Wouldn’t it just be easier to order your results in descending order by the primary key and limit it to a single result?
Saves looping through all of the data then.
Yeah, that’s really more elegant! However, what happens when I have only 6 rows in the table? Will the query do the job in that case as well?
You’re right, it’s not.
You need the use LIMIT as I posted earlier.
<?php
$result = mysql_query(
'SELECT date FROM table ORDER BY id DESC LIMIT 9, 1'
);
$row = mysql_fetch_assoc($result);
echo $row['date'];
?>
Well, thank you guys! I’ve tried to modify the query, but I couldn’t get what I want…
Please have a look at the script :
$q_inter = mysql_query("SELECT date FROM interview_questions WHERE asker_id = '".$_SESSION['user_id']."' AND answerer_id = '$contact_to_add' ORDER BY id DESC LIMIT 3");
$nr_of_int = mysql_num_rows($q_inter);
for ($i=0; $i<$nr_of_int; $i++) {
$row = mysql_fetch_array($q_inter);
$var[ ] = $row;
}
echo "<pre>";
print_r ($var);
echo "</pre>";
with this I get at the browser:
Array
(
[0] => Array
(
[0] => 2010-09-25
[date] => 2010-09-25
)
[1] => Array
(
[0] => 2010-09-20
[date] => 2010-09-20
)
[2] => Array
(
[0] => 2010-09-18
[date] => 2010-09-18
)
)
Can you guys now help me to retrieve/echo/whatever the LAST date whis is in this case 2010-09-18? I appreciate your expertise!