Closed Thread Icon

Topic awaiting preservation: Database fun (The second PHP thread in the hateful answers series) (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=22547" title="Pages that link to Topic awaiting preservation: Database fun (The second PHP thread in the hateful answers series) (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Database fun (The second PHP thread in the hateful answers series) <span class="small">(Page 1 of 1)</span>\

 
Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-14-2004 02:10

Once again, I was hesitant to ask this, but every tutorial I find falls just short of providing the information I need.

I have a MySQL database which I have succesfully provided with a tableful of information. I have the means to call and reference the data, but my requirements are a little more specialized. I need to list five instances of information (essentially I would print the title field) in order from newest to oldest. I have incremental IDs, but I have no clue how to print 5 instances, with greatest ID to smallest ID.

Thank you for aiding my newbie campaign. Have a good day.

code-headed intern

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 07-14-2004 03:07

Alexer: Have you had a look at the between, in, order by and limit clauses ?

kronk
Nervous Wreck (II) Inmate

From: Sydney, Australia
Insane since: Jun 2004

posted posted 07-14-2004 15:16

you could try the following:

1.. query the db with whatever you need, i.e. for the five instances you want
2.. order by id descending, this will give output in 10 -> 1 style
3.. load them into an associative array
4.. print them into a table using a while loop

if you me to be more specific (with code egs) let me know

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-14-2004 19:10

Is there a php equivalent to orderby? Say I query like so:

code:
$Query = "Select * from $tablename";
$articles = mysql_db_query ($dbname, $Query, $Link);



So, now I have everything from $tablename (which has been defined earlier.) Now, I just need to print the correct instances into a table. I understand how I could use orderby, but I need a php equivalent. Thank you

code-headed intern

Tyberius Prime
Paranoid (IV) Mad Scientist with Finglongers

From: Germany
Insane since: Sep 2001

posted posted 07-14-2004 19:22

php->sort(), php->usort(), php->ksort() all could be used after you read your data into an array - but why can't you use order by??

kronk
Nervous Wreck (II) Inmate

From: Sydney, Australia
Insane since: Jun 2004

posted posted 07-15-2004 11:33

to get the result of the query in the correct order, i think you just add the 'order by ???' to the end of your quewry like this:

$query = "select * from $tablename order by id desc";

from what i know, you should then be able to load the answer into an array like this:

$articles = mysql_db_query ($dbname, $query, $link);

while ($row = mysql_fetch_array($articles)) {
echo "print table or what ever here for each item in the array";
}

hope this helps

kronk
Nervous Wreck (II) Inmate

From: Sydney, Australia
Insane since: Jun 2004

posted posted 07-15-2004 11:45

i think you can just add the 'order by ???' to the end of your query like this:

$query = "select * from $tablename order by id desc";

then to print the result try this:

$articles = mysql_db_query($dbname, $query, $link);

while ($row = mysql_fetch_array($articles, MYSQL_ASSOC) {
echo "print table rows here or similar";
}

hope this helps

kronk
Nervous Wreck (II) Inmate

From: Sydney, Australia
Insane since: Jun 2004

posted posted 07-15-2004 13:03

sorry about the double entry i am having trouble with my isp dropping out tonight

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-16-2004 00:32
code:
$query = "select * from $tablename order by id desc";



This worked perfectly. For some reason I was under the impression that "order by" would force me into a foray of another language, opening up multiple cans of worms that had been untouched for a millenia. Anyway...

As for limiting my table to five entries, I simply introduced a variable called $facecount . I then modified my while statement so...

code:
while (($row = mysql_fetch_array($result))&&($facecount < 5)) {

//A bunch of table stuff here..

$facecount++;
}



Incrementing $facecount in each runthrough was exactly what I needed to make sure the loop ended at the right moment, and doing order by id descending was exactly what I needed to keep it in order.

Thank you all for your help. I don't know what I'd do in this job without Ozone Asylum.

code-headed intern

(Edited by Alexer on 07-16-2004 00:33)

poi
Paranoid (IV) Inmate

From: France
Insane since: Jun 2002

posted posted 07-16-2004 00:40

you know you can use an order by clause AND a limit clause in the same query.


[edit] fixed a typo [/edit]

(Edited by poi on 07-16-2004 01:50)

H][RO
Bipolar (III) Inmate

From: Australia
Insane since: Oct 2002

posted posted 07-16-2004 01:47

yeh i have to do what youve done above for an access database, but sql has a limit clause you can use. Would be handy too

Alexer
Nervous Wreck (II) Inmate

From: Juneau, Alaska
Insane since: Jun 2004

posted posted 07-16-2004 17:46

I wasn't aware of the limit clause. I'm new to programming altogether, and I've never used MySQL except for that orderby statement. Anyway, thanks for pointing that out

code-headed intern

« BackwardsOnwards »

Show Forum Drop Down Menu