Make an 'echo' random in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1330

    #1

    Tech Make an 'echo' random in php?

    $title = $row['Name'];
    I'm using the above echo statement to insert data into a page however, it dawned on me that it could be better to pull from a larger selection of data from within that same defined column, is this possible to achieve using a simple echo statement like the one above, just with a little longer statement, that doesnt involve adding SQL queries?

    I'm thinking it must be possible using something along the lines of this, maybe just to give an idea of what I'm looking for?

    $title = $row['Name'] RAND();
    <?php echo $title; random ?>
    Is this possible?

    TIA for any assistance.
    Extreme Link List - v1.0
  • NoWhErE
    Too lazy to set a custom title
    • Sep 2005
    • 10583

    #2
    you'd have to save all the names as an array. I don't know if your data is available in that format, but if it is, all you need to do is $random_title = array_rand($your_array_of_names) and it will output a random result every time.

    if your data isn't an array yet, you can create one by adding each name in your database like this

    step 1: create an empty array
    $array = array();

    step 2: go through each name in your db and add it to the array
    foreach($row as $name) {
    $array[] = $name;
    }

    now your array is ready to be randomized.
    skype: lordofthecameltoe

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1330

      #3
      Originally posted by NoWhErE
      you'd have to save all the names as an array. I don't know if your data is available in that format, but if it is, all you need to do is $random_title = array_rand($your_array_of_names) and it will output a random result every time.

      if your data isn't an array yet, you can create one by adding each name in your database like this

      step 1: create an empty array
      $array = array();

      step 2: go through each name in your db and add it to the array
      foreach($row as $name) {
      $array[] = $name;
      }

      now your array is ready to be randomized.
      Much appreciated.

      Thats what I was afraid of, itll take forever to setup the individual arrays, looks like I need to recode using SQL Queries instead

      Thanks!
      Extreme Link List - v1.0

      Comment

      • NoWhErE
        Too lazy to set a custom title
        • Sep 2005
        • 10583

        #4
        Originally posted by Publisher Bucks
        Much appreciated.

        Thats what I was afraid of, itll take forever to setup the individual arrays, looks like I need to recode using SQL Queries instead

        Thanks!
        It depends. Either way, with SQL queries or with arrays, it should only be a few lines of code at most to get the data you want.
        skype: lordofthecameltoe

        Comment

        Working...