Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 5 of 5

Thread: [PHP] Array Problems, man I'm dumb lately

  1. #1

    [PHP] Array Problems, man I'm dumb lately

    Hey guys, I have this code

    PHP Code:
    $rowsy mysql_query " SELECT * FROM mainpage WHERE page = 'Random'" );
    $IDS = array ("17");
    while (
    $rows mysql_fetch_assoc($rowsy)) {
    $IDS[] =  $rows['ID'] ;
    }
    $numRows mysql_num_rows($rowsy);
    $real $numRows 1;
    $random rand(0,$rea);
    $final $IDS[$random];
    $query17 mysql_query" SELECT * FROM mainpage WHERE ID = '$final'" ); 
    Which should..
    Find the amount of rows in the table with the page set as 'Random'
    Which it does
    Then it should create an array with those ID's.
    Then it should find the rows again, this time subtracting one to correspond to the array
    Then, find a random number between zero and the amount of rows, which I'm not really sure if it does
    Then it should take that number and use it to pull an ID out of the array, which I'm pretty sure it doesn't do.
    Finally, it should pull use that ID to pull out the info from the array.

    Not working.

    Any brilliant ideas, I'm getting frustrated here.

    Yeah, I realize this is a really stupid way to do this, but it's about my only option with the way I set up the table like a moron.

  2. #2
    Just a little helpful info.

    $random always outputs 0.
    So the problem is somewhere around there I guess

  3. #3
    Quote Originally Posted by xxviii
    Hey guys, I have this code

    PHP Code:
    $rowsy mysql_query " SELECT * FROM mainpage WHERE page = 'Random'" );
    $IDS = array ("17");
    while (
    $rows mysql_fetch_assoc($rowsy)) {
    $IDS[] = $rows['ID'] ;
    }
    $numRows mysql_num_rows($rowsy);
    $real $numRows 1;
    $random rand(0,$rea);
    $final $IDS[$random];
    $query17 mysql_query" SELECT * FROM mainpage WHERE ID = '$final'" ); 
    just check ur code variables, especially the line

    PHP Code:
     $random rand(0,$rea); 
    try it as

    PHP Code:
     $random rand(0,$real); 
    edit:-
    if you want it to be random...

    in your sql code

    Code:
     ORDER BY rand() LIMIT 1

  4. #4
    Quote Originally Posted by binime
    if you want it to be random...

    in your sql code

    Code:
     ORDER BY rand() LIMIT 1

    yeah... let mysql do the job... much easier

  5. #5
    Whoa thanks guys, didn't see anyone responded. I got it to work my stupid cracked up way, I had no idea I could put rand() inside an sql query like that. Very cool, thanks a lot

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012