Results 1 to 9 of 9

Thread: Help with array

  1. #1
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default Help with array

    Hi folks,I found this script by Dave,he does not provide support for this script.

    I am trying to add a third part to this array.This is the original script.

    PHP Code:
    <?
    //////////////////////////////////////////////////////
    //
    // This is a VERY simple PHP Random Quote generator.
    //   I provide the source code for anyone who wants it for free.
    //   I also provide NO support for this program as well...
    //   Use it at your own risk ;-)
    //     If you use the script, please send me an email that says "Thanks"
    //     and what the URL is for your website. <quotes_nospam@davidmarotz.com>
    //
    //////////////////////////////////////////////////////

    $bgcolor '#FFFFCC';
    $textcolor 'black';
    $textsize '6';

    // Array Structure:  "Quote","Author"

    $allqts = array
        (
    "The Black Knight Always Triumphs!"
            
    "Monty Python",
        
    "I swear by my life and love of it that I will never live for the sake of<br>another man, nor ask another man to live for mine" ,
            
    "Atlas Shrugged",
        
    "It is clear that the individual who persecutes a man, his brother,<br> because he is not of the same opinion, is a monster",
            
    "Voltaire",
        
    "I agree that there is a natural aristocracy among men<br>The grounds of this are virtue and talents.",
            
    "Thomas Jefferson",
        
    "Liberty, when it begins to take root, is a plant of rapid growth."
            
    "George Washington",
        
    "Never argue with an idiot. <br>They drag you down to their level <br>then beat you with experience",
            
    "Dilbert",
        
    "The Answer is 42. What is the question?",
            
    "Hitchikers Guide to the Galaxy",
        
    "Anyone who has never made a mistake has never tried anything new",
            
    "Albert Einstein",
        
    "Progress doesn't come from early risers, progress is made<br>by lazy men looking for easier ways to do things.",
            
    "Lazarus Long <font size=-2>(Time Enough for Love by Robert A. Heinlein)</font>",
        
    "Throughout history, poverty is the normal condition of man. <br>Advances which permit this norm to be exceeded - here and there, now and then - <br>are the work of an extremely small minority, frequently despised,<br> often condemned, and almost always opposed by all right-thinking people. <br>Whenever this tiny minority is kept from creating, or (as sometimes happens)<br> is driven out of a society, the people then slip back into abject poverty.<br><br>This is known as 'bad luck.'",
            
    "Robert Heinlein",
        
    "A little learning is a dangerous thing; Drink deep, or taste not the<br>Pierian spring.  There shallow draughts intoxicate the brain, <br>and drinking largely sobers us again",
            
    "Alexander Pope",
        
    "The early bird gets the worm, but the second mouse gets the cheese",
            
    "Anonymous",
        
    "Subjugating the enemy's army without fighting is the true pinnacle of excellence",
            
    "Sun-tzu, The Art of War",
        
    "Work as though you were to live 100 years; pray as if you were to die tomorrow",
            
    "Benjamin Franklin",
        
    "The world is a stage, but the play is badly cast",
            
    "Oscar Wilde",
        
    "Truth is generally the best vindication against slander.",
            
    "Abraham Lincoln",
        
    "...mercy to the guilty is cruelty to the innocent...",
            
    "Adam Smith",
        
    "...I wish that I may never think the smiles of the great and powerful<br> a sufficient inducement to turn aside from the straight path<br> of honesty and the convictions of my own mind",
            
    "David Ricardo",
        
    "Democracy is the worst form of government except for all the others",
            
    "Winston Churchill",
        
    "You can only know the highest peaks if you have experianced the lowest valley's",
                
    "Richard Nixon",
        
    "They dress the wound of my people as though it were not serious. <br>'Peace, peace,' they say, when there is no peace.",
            
    "Jeremiah 6:14",
        
    "It is better to remain silent and be thought a fool<br> than to open your mouth and remove all doubt.",
            
    "Jonathan Swift",
        
    "The market system delivers the goods people want,<br>but those who make it work cannot readily explain why it is so.<br>The socialst or communist system does not deliver the goods, <br>but those who operate it can readily explain away its failure.",
            
    "F.A. Hayek, Law, Legislation and Liberty, Vol. II",
        
    "Never Stop Exploring<sup>tm</sup>",
            
    "The North Face"
            
    );

    // Gets the Total number of Items in the array
    //  Divides by 2 because there is a Quote followed by an Author
    $totalqts = (count($allqts)/2);

    // Subtracted 1 from the total because '0' is not accounted for otherwise
    $nmbr = (rand(0,($totalqts-1)));
    $nmbr $nmbr*2;

    //$nmbr = 18;

    $quote $allqts[$nmbr];
             
    $nmbr $nmbr+1;
    $author $allqts[$nmbr];

    // You can delete this section
    //   it is only so Search engines can find it
    if ($PHP_SELF == "/quotes.php") {
        echo 
    "<Title>Random Quote Script for PHP</title>";
        echo 
    "<meta name=\"Description\" content=\"Random Quote script written in PHP\">";
        echo 
    "<meta name=\"keywords\" content=\"Random Quote script written in PHP\">";
        echo 
    "This is my random quote script that is written in PHP";
        echo 
    "<br>Feel free to use it for whatever you want<br/><br/>";
    }
    /// End Delete

    $space "<font color=$bgcolor>.....................................</font>";
    $comments "<br><center><font size='4'><i><a href='quotes.php'>Random Quote Script by Dave</a></i></font></center>";

    echo 
    "<center>";
    echo 
    "<Font color=$textcolor size='$textsize'><i>";
    echo 
    "$quote<br>";
    echo 
    "</i></font>";
    echo 
    "$space $author";
    echo 
    "$comments";
    echo 
    "</center>";

    IF (
    $_SERVER['PHP_SELF'] == "/quotes.php") {
        
    show_source("quotes.php");
       }

    ?>
    And this is what I have.It is not showing the image:
    [PHP]

    my current version is here:
    http://www.web-user.info/quotes/quotes1/aquote.txt

    (The limit was exceded so I posted a text file of what I have as of now)

    Can someone show a newbie(at php) what he is doing wrong?

    I want to have the quote,author and image all work as the are in the script-an image set to quote/author.So far I am unable to get the image to show up
    I hope to use css to align the text on the image once the image is displayed.

    thanks
    Last edited by ajfmrf; 04-13-2014 at 12:04 AM.
    Thanks,

    Bud

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    PHP Code:
    <?php
    //////////////////////////////////////////////////////
    //
    // This is a VERY simple PHP Random Quote generator.
    //   I provide the source code for anyone who wants it for free.
    //   I also provide NO support for this program as well...
    //   Use it at your own risk ;-)
    //     If you use the script, please send me an email that says "Thanks"
    //     and what the URL is for your website. <quotes_nospam@davidmarotz.com>
    //
    //////////////////////////////////////////////////////

    $bgcolor 'white';
    $textcolor 'black';
    $textsize '4';

    // Array Structure:  "Quote","Author","image"

    $allqts = array
    (
    "The Black Knight Always Triumphs!""Monty Python","images/1b.jpg",

    "I swear by my life and love of it that I will never live for the sake of<br>another man, nor ask another man to live for mine" ,"Atlas Shrugged","images/2b.jpg",

    "It is clear that the individual who persecutes a man, his brother,<br> because he is not of the same opinion, is a monster","Voltaire","images/3b.jpg",

    "I agree that there is a natural aristocracy among men<br>The grounds of this are virtue and talents.","Thomas Jefferson","images/4b.jpg",

    "Liberty, when it begins to take root, is a plant of rapid growth.","George Washington","images/5b.jpg",

    "Never argue with an idiot. <br>They drag you down to their level <br>then beat you with experience","Dilbert","images/6b.jpg",

    "The Answer is 42. What is the question?","Hitchikers Guide to the Galaxy","images/7b.jpg",

    "Anyone who has never made a mistake has never tried anything new","Albert Einstein","images/8b.jpg",

    "Progress doesn't come from early risers, progress is made<br>by lazy men looking for easier ways to do things.","Lazarus Long <font size=-2>(Time Enough for Love by Robert A. Heinlein)</font>","images/9b.jpg",

    "Throughout history, poverty is the normal condition of man. <br>Advances which permit this norm to be exceeded - here and there, now and then - <br>are the work of an extremely small minority, frequently despised,<br> often condemned, and almost always opposed by all right-thinking people. <br>Whenever this tiny minority is kept from creating, or (as sometimes happens)<br> is driven out of a society, the people then slip back into abject poverty.<br><br>This is known as 'bad luck.'",
    "Robert Heinlein","images/10b.jpg",

    "A little learning is a dangerous thing; Drink deep, or taste not the<br>Pierian spring.  There shallow draughts intoxicate the brain, <br>and drinking largely sobers us again","Alexander Pope","images/11b.jpg",

    "The early bird gets the worm, but the second mouse gets the cheese","Anonymous","images/12b.jpg",

    "Subjugating the enemy's army without fighting is the true pinnacle of excellence",
    "Sun-tzu, The Art of War","images/13b.jpg",

    "Work as though you were to live 100 years; pray as if you were to die tomorrow",
    "Benjamin Franklin","images/14b.jpg",

    "The world is a stage, but the play is badly cast","Oscar Wilde","images/15b.jpg",

    "Truth is generally the best vindication against slander.","Abraham Lincoln","images/16b.jpg",

    "...mercy to the guilty is cruelty to the innocent...","Adam Smith","images/17b.jpg",

    "...I wish that I may never think the smiles of the great and powerful<br> a sufficient inducement to turn aside from the straight path<br> of honesty and the convictions of my own mind","David Ricardo","images/18b.jpg",

    "Democracy is the worst form of government except for all the others","Winston Churchill","images/19b.jpg",

    "You can only know the highest peaks if you have experianced the lowest valley's",
    "Richard Nixon","images/20b.jpg",

    "They dress the wound of my people as though it were not serious. <br>'Peace, peace,' they say, when there is no peace.","Jeremiah 6:14","images/1b.jpg",

    "It is better to remain silent and be thought a fool<br> than to open your mouth and remove all doubt.","Jonathan Swift","images/2b.jpg",

    "The market system delivers the goods people want,<br>but those who make it work cannot readily explain why it is so.<br>The socialst or communist system does not deliver the goods, <br>but those who operate it can readily explain away its failure.","F.A. Hayek, Law, Legislation and Liberty, Vol. II","images/3b.jpg",

    "Never Stop Exploring<sup>tm</sup>","The North Face","images/5b.jpg"
            
    );

    // Gets the Total number of Items in the array
    //  Divides by 3 because there is a Quote followed by an Author,then img
    $totalqts = (count($allqts)/3);

    // Subtracted 1 from the total because '0' is not accounted for otherwise
    $nmbr = (rand(0,($totalqts-1)));
    $nmbr $nmbr*3;

    //$nmbr = 18;

    $quote $allqts[$nmbr];
             
    $nmbr $nmbr+1;
    $author $allqts[$nmbr];
    $image $allqts[$nmbr+1];

    $space "<font color=$bgcolor>.....................................</font>";
    $comments "<br><center><font size='-2'><i><a href='quotes.php'>Random Quote Script by Dave</a></i></font></center>";

    echo 
    "<center>";
    echo 
    "<Font color=$textcolor size='$textsize'><i>";
    echo 
    "$quote<br>";
    echo 
    "</i></font>";
    echo 
    "$space $author<br>";
    echo 
    "<img src=\"$image\">";
    echo 
    "$comments";

    echo 
    "</center>";



    IF (
    $_SERVER['PHP_SELF'] == "/quotes.php") {
        
    show_source("quotes.php");
       }

    ?>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ajfmrf (04-11-2014)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    John's answer is what you asked for, but I might suggest a much simpler (and more efficient, more robust) version instead of simply modifying the existing script:

    PHP Code:
    <?php

    // use an array of arrays
    // this way, each quote+author+image is implicitly grouped
    // (and won't get mixed up if you forget one, or do math wrong, or something)
    $quotes = array(
        array( 
    "quote the first",    "Author Name","/path/to/image.jpg" ),
        array( 
    "quote the second","Author"         ,"/path/to/image.jpg" ),
        
    //  etc. ...
    );
    // choose a random entry
    $randomKey array_rand$quotes );
    // extract the values from that entry
    list( $quote,$author,$image ) = $quotes[$randomKey];

    // print them (in whatever format you prefer, but as an example):
    print <<< HTML
    <blockquote class="random-quote">
        <p>
    $quote</p>
        <cite>
    $author</cite>
        <img src="
    $image">
    </blockquote>
    HTML;
    You can use the classname to style via CSS.

  5. The Following User Says Thank You to traq For This Useful Post:

    ajfmrf (04-11-2014)

  6. #4
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Well, I want to thank both of you John and Traq.

    Both of you have gievn me a great way to do the same thing.

    John,I was pretty close to what you have but the image thing was fouling me up.

    Now,I will get to styling the page.Then the last thing.make it so it rotates quotes automaticly without reload.

    I am going to attempt that next and will start a new thread if I can't get it on my own.

    Traq,I need to look your code over and see how it works-it is quite different looking then what I was trying(and John ) did.
    Thanks,

    Bud

  7. #5
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    Traq,I get this error when I use your version:

    Parse error: syntax error, unexpected $end in /home/webuseri/public_html/quotes/quotes1/traq.php on line 34

    If the line is correct,that line is the last part:

    HTML;

    That's if the line count is correct.
    Thanks,

    Bud

  8. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    That last part is a special kind of string called a HEREDOC. It starts with <<< and an arbitrary word ("HTML", in this case) and ends with that same word, at the beginning of its own line, and with no other characters on that line (except the semicolon). So, if that last HTML; is indented, or has any other characters on the same line, you'll get the error you described above.

    I've tested the code and it works fine for me. The problem I described above is very common, though.

    Depending on what version of PHP you have (what version of PHP do you have?), it might be another problem as well.

    If you prefer, you can also use a regular, double- or single-quoted string and concatenate the variables. I like heredocs because you can use both double- and single-quotes inside the string without escaping them, and you still get to use variables too. It's very nice for formatting HTML. But regular strings will give you the same result.

  9. #7
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    I have php version 5.3.3

    and as far as I see in my editor ,there are no spaces,indents or anything else in front or back of the HTML;

    As far as the rest of your post,for me,it might as well be in Klingon-I don't follow you at all.

    I am just trying to figure out this script and am a newbie at php in doing so.

    I am going to look again at this script and hope I was wrong and there is something like what you are talking about Trac.

    thanks
    Thanks,

    Bud

  10. #8
    Join Date
    Jan 2011
    Location
    Southeastern CT
    Posts
    612
    Thanks
    46
    Thanked 32 Times in 32 Posts

    Default

    here is a test page for the script Trac

    http://www.web-user.info/quotes/quotes1/traq.txt

    I don't see any space one the end line with HTML; on it

    I don't know what is going on.....

    did I do the link right?
    Last edited by ajfmrf; 04-11-2014 at 11:02 PM. Reason: edit link
    Thanks,

    Bud

  11. #9
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by ajfmrf View Post
    the rest of your post,for me,it might as well be in Klingon…
    nuqneH?
    tlhIngan Hol Dajatlh'a'?

    Quote Originally Posted by ajfmrf View Post
    here is a test page for the script Trac http://www.web-user.info/quotes/quotes1/traq.txt
    I tried that exact script; it runs fine, with no errors.

    Quote Originally Posted by ajfmrf View Post
    I have php version 5.3.3
    You might try putting the semicolon on the line below, i.e.:
    PHP Code:
    print <<< HTML
    <blockquote class="random-quote">
        <p>
    $quote</p>
        <cite>
    $author</cite>
        <img src="
    $image">
    </blockquote>
    HTML

    Supposedly, that cannot possibly cause problems, but I've found it actually happens on some versions on some servers. Don't know why.

    Alternatively, you could just use regular strings and concatenate the variables:
    PHP Code:
    print '<blockquote class="random-quote">
        <p>'
    $quote .'</p>
        <cite>'
    $author .'</cite>
        <img src="'
    $image .'">
    </blockquote>'


Similar Threads

  1. Replies: 3
    Last Post: 03-01-2012, 04:42 PM
  2. push array into array in a different file
    By baconDelta in forum PHP
    Replies: 12
    Last Post: 01-06-2012, 01:05 AM
  3. Resolved POST array empty, but GET array has data
    By Schmoopy in forum PHP
    Replies: 4
    Last Post: 06-14-2011, 08:22 PM
  4. Resolved Attach txt array to image array???
    By 03SF06 in forum JavaScript
    Replies: 5
    Last Post: 03-07-2009, 05:40 PM
  5. Replies: 5
    Last Post: 08-20-2007, 03:02 AM

Bookmarks

Posting Permissions

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