Getting random numbers

Discussion in 'PHP' started by enchance, Oct 10, 2008.

  1. #1
    How do you get 3 random numbers from 1-20 without repeating any of the already selected numbers?
     
    enchance, Oct 10, 2008 IP
  2. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think I got it. But why won't it work?
    
    <?php
    $quotes = simplexml_load_file("quotes.xml") or die ("Whoops!"); //line 180 index.php
    $max = count($quotes->quote); //zero-based
    $numbers = range(0, $max);
    shuffle($numbers);
    
    //loop with the necessary code
    for($i = 0; $i <= 2; $i++){
    echo <<<END
    <li><span class="quote">$quotes->quote[$numbers[$i]]</span><img class="quotearrow" src="../_media/_global/testimonials-arrow.gif" /><span class="quoteby"><h4>$quotes->quote[$numbers[$i]]['sayswho']</h4>$quotes->quote[$numbers[$i]]['whoisthe']</span></li>
    END;
    }
    ?>
    
    PHP:
    I'm getting this error
    
    Warning: simplexml_load_file() [function.simplexml-load-file]: quotes.xml:3: parser error : xmlParseEntityRef: no name in D:\xampp\htdocs\somesite\index.php on line 180
    
    PHP:
     
    enchance, Oct 10, 2008 IP
  3. buldozerceto

    buldozerceto Active Member

    Messages:
    1,137
    Likes Received:
    43
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Have you tried with the PHP function mt_rand()?

    mt_rand(1,20);
     
    buldozerceto, Oct 10, 2008 IP
  4. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sounds like an option. Do you mind telling me what's wrong with my code above? I'm really at a loss.
     
    enchance, Oct 10, 2008 IP
  5. Exia

    Exia Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can I see your .XML file? Sounds like you have & instead of &amp; in there.
     
    Exia, Oct 10, 2008 IP
  6. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here's the file quotes.xml. It's just placeholder text for now but it doesn't work.
    
    <?xml version="1.0" encoding="utf-8"?>
    <testimonials>
    	<quote sayswho="JM" whoisthe="King">I’m still using Ubuntu Edgy Eft with the Ubuntu Studio Theme + Neutronium Theme. ... Ubuntu & Windows XP. Where did you learn that kind of dual boot?</quote>
    	<quote sayswho="Jim Lee" whoisthe="Joker">I want to dual boot linux and XP on my laptop (maybe thinking about ... I believe Ubuntu Studio (as far as I'm aware) is Ubuntu with ALOT of ...</quote>
    	<quote sayswho="Minimo" whoisthe="Loophole">The Ubuntu Studio theme is a cool and amazing theme for your Ubuntu! .... Make Ubuntu look like Vista · How To Prevent Windows XP Users From Writing To US.</quote>
    </testimonials>
    
    Code (markup):
    Actually, can anyone show me how to access the information in this xml file including its attributes?
     
    enchance, Oct 11, 2008 IP
  7. Exia

    Exia Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    As a quick test, can you try removing the ' from "I'm" in the first quote, then try your script again.
     
    Exia, Oct 12, 2008 IP
  8. Greg Carnegie

    Greg Carnegie Peon

    Messages:
    385
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    That's a good idea, but if that won't help try replacing & with &amp;
     
    Greg Carnegie, Oct 12, 2008 IP
  9. Exia

    Exia Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I said that in a previous post, but didn't see the & in his XML, I must not of been paying attention. :)

    Greg is correct, the & will parse as malformed XML - you should use &amp; (ampersand) instead.
     
    Exia, Oct 12, 2008 IP