Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,175 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,201 people online right now. Registration is fast and FREE... Join Now!




randomizing entries from database with php

 
Reply to this topicStart new topic

randomizing entries from database with php

irishgirl
28 Aug, 2008 - 06:57 AM
Post #1

D.I.C Head
**

Joined: 22 Aug, 2008
Posts: 68


My Contributions
I have a category on my site. The range in a particular category is also available in two other categories. I need a link to say "Also available in Category1 (5 items) and Category3 (1 item)" dynamically. At the moment I have it working from form entry which is saved in database and then outputted by me but I've noticed the links are the same on each page (even if you're on the page with Category3, the link will say Category1 and Category3) and i'd like these randomized so the current link displaying is not included in the link on the page. Make sense? :s

This is what Im doing at the moment:

CODE

$result2=mysql("$DBName","SELECT * FROM range");
    while($row2=mysql_fetch_row($result2)){
        $rid = $row2[0];
        $range=$row2[1];
        $details=$row2[2];
        $availablelink1=$row2[5];
        $availablelinktitle1=$row2[6];
        $itemsfromlink1=$row2[7];
        $availablelink2=$row2[8];
        $availablelinktitle2=$row2[9];
        $itemsfromlink2=$row2[10];

        if ( $itemsfromlink1 == 1) { $itemfromlink1 = $itemsfromlink1 . ' item'; } else { $itemfromlink1 = $itemsfromlink1 . ' items'; }
        if ( $itemsfromlink2 == 1) { $itemfromlink2 = $itemsfromlink2 . ' item'; } else { $itemfromlink2 = $itemsfromlink2 . ' items'; }


if ( $availablelink2 && $availablelinktitle2 && $itemsfromlink2 == '' && $availablelink1 && $availablelinktitle1 && $itemsfromlink1 != '')
{ echo "<p>Also available for <a href=\"$availablelink1\">$availablelinktitle1 ($itemfromlink1)</a>"; }

                    
if ( $availablelink1 && $availablelinktitle1 && $itemsfromlink1 && $availablelink2 && $availablelinktitle2 && $itemsfromlink2 != '')

{ echo "<p>Also available for <a href=\"$availablelink1\">$availablelinktitle1 ($itemfromlink1)</a> and <a href=\"$availablelink2\">$availablelinktitle2 ($itemfromlink2)</a></p>"; }
                    

echo "</div></div>";


This post has been edited by irishgirl: 28 Aug, 2008 - 07:55 AM
User is offlineProfile CardPM
+Quote Post

mocker
RE: Randomizing Entries From Database With Php
28 Aug, 2008 - 07:23 AM
Post #2

D.I.C Regular
Group Icon

Joined: 14 Oct, 2007
Posts: 290



Thanked: 17 times
Dream Kudos: 25
My Contributions
QUOTE(irishgirl @ 28 Aug, 2008 - 07:57 AM) *


CODE

        $available1=$row2[5];
        $avail1=$row2[6];
        $available2=$row2[8];
        $avail2=$row2[9];



I really suggest developing a standard for how you name your variables. Maybe you can keep in your head what the difference is between these variables, but once you post it or someone else needs to look at it, we have no idea.

Same with item1 items1.. that took me like 5 minutes to spot that you were using different variables.


If you want to skip the current page, you need to have somewhere in your script defined what the current page is (which you either dont or dont show), then you can change the query so its

$result2=mysql("$DBName","SELECT * FROM range WHERE not(FIELD_NAME = $CURRENT_PAGE)");

with the appropriate field name of course for whatever available1 and available 2 are supposed to be
User is offlineProfile CardPM
+Quote Post

Ridikule
RE: Randomizing Entries From Database With Php
28 Aug, 2008 - 12:28 PM
Post #3

D.I.C Head
**

Joined: 29 Sep, 2005
Posts: 58



Thanked: 4 times
My Contributions
If you want to select random rows, you can use the database the randomize the result set:

SELECT * FROM `range` WHERE 1 ORDER BY RAND() LIMIT 1;

This is actually on this site in the snippets:
http://www.dreamincode.net/code/snippet251.htm

Of course, you'll have to change the limit to however many results you want to return.




User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month