Results 1 to 10 of 10

Thread: PHP loop help

  1. #1
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default PHP loop help

    Holy crap he is back!

    Yes, but I have a problem I am trying to rectify. I am trying to write a PHP script for my job, where I have to post to craigs list.

    Now I have a template I created where I make changes to the cars that are being posted. So I use my template and change, year, make model, information etc etc etc... For each and every car.

    So I thought, why not automate it? And I have. I wrote a script with some input boxes, and when you fill them in and click submit, it adds the inputted info into the echo calls and displays the template ready for me to copy and paste.

    All done, all good.

    But now I am having an issue with my images. Because the number of images changes with each car (some have 27 some have 32, some have 34.. some have 40 etc...

    I need to create a loop using 2 variables.. baseURL and imgNum. I need the script to run through and echo the $baseURL and &imgNum for each image.

    So if I input the image number on the form as "43" then I need the loop to run that many times.

    Easy right? Not really, there are 2 main things to keep in mind.. each number DOES start with 1, but it's 3 characters (001) so image # 43 would be 043 etc...

    Second, I need to add this: "</td><td>" AFTER every 3rd echo, to keep the template looking how it looks.

    The script is right here

    You can enter any info you want, there are only length restrictions, and when submitted the code to copy appears.

    I don't care about security, safety, proper formatting, etc.. as I am the only one that will use this, and no one will ever even see it but me. I just want to be able to copy and paste into craigs list rather than write all this each time.

    Ideas?
    Last edited by BLiZZaRD; 03-13-2010 at 09:30 PM.
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Something like this?

    PHP Code:
    $page_content "";
    $image_num $_POST['imgNum'];
    $baseurl $_POST['baseURL'];
    for (
    $a 1$a $image_num$a++) {
        if (
    $a 100 && $a 10) {
            
    $place_holder "0";
        } else {
            
    $place_holder "";
        }
        if (
    $a 10) {
            
    $place_holder "00";
        }
        
    $img_tag "<img src=\"$baseurl$place_holder$a\" alt=\"Figure $place_holder$a\" width=\"325\" height=\"250\" />\n"
        if (
    $a == 1) {
        
    $page_content .= "<td>$img_tag";
        }
        if (
    $a ==) {
            
    $page_content .= "</td>\n<td>$img_tag";
        } else {
            
    $page_content .= "$img_tag";
        }

    Corrections to my coding/thoughts welcome.

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

    BLiZZaRD (03-11-2010)

  4. #3
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Thanks! I like the idea of place holder. I will look at the concept closely and try it out.

    Appreciate the input and the head start!
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  5. #4
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Okay, had to make some slight changes. Nothing big (added ".jpg" to the URL, changed the $img_tag = line to display html code (so it doesn't parse).

    Here is what I have now:

    PHP Code:
    for ($a 1$a $imgNum$a++) {
        if (
    $a 100 && $a 9) {
            
    $place_holder "0";
        } else {
            
    $place_holder "";
        }
        if (
    $a 10) {
            
    $place_holder "00";
        }
        
    $img_tag "&lt;img src=&quot;$baseURL$place_holder$a.jpg&quot; width=&quot;325&quot; height=&quot;250&quot; /&gt;\n"
        if (
    $a == 1) {
        
    $page_content .= "<td>$img_tag";
        }
        if (
    $a == ) {
            
    $page_content .= "</td><td>\n$img_tag";
        } else {
            
    $page_content .= "$img_tag";
        }
    }
    echo 
    $page_content
    And it works.. kind of. The display is there, but image 1 (001) is there twice before it moves to image 002. Also, there is no line break after each $img_tag, nor are there any <td> or <tr> at all. Here is a copy of the output when the script is run:

    HTML Code:
    <img src="http://www.autobase.com/photos/00320/1057/10570164_001.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_001.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_002.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_003.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_004.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_005.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_006.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_007.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_008.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_009.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_010.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_011.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_012.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_013.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_014.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_015.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_016.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_017.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_018.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_019.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_020.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_021.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_022.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_023.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_024.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_025.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_026.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_027.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_028.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_029.jpg" width="325" height="250" /> <img src="http://www.autobase.com/photos/00320/1057/10570164_030.jpg" width="325" height="250" /><img src="http://www.autobase.com/photos/00320/1057/10570164_031.jpg" width="325" height="250" /></table>
    I can deal with the no line breaks. No big deal. But I need the </tr><tr> after every 3rd one. Thats a must.

    Ideas?
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    Apologies for the triple post...

    I am now using this code (slight tweaks, nothing major)

    PHP Code:
    for ($a 1$a $imgNum$a++) {
        if (
    $a 100 && $a 9) {
            
    $place_holder "0";
        } else {
            
    $place_holder "";
        }
        if (
    $a 10) {
            
    $place_holder "00";
        }
        
    $img_tag "&lt;td align=&quot;center&quot;&gt;&lt;img src=&quot;$baseURL$place_holder$a.jpg&quot; width=&quot;325&quot; height=&quot;250&quot; /&gt;&lt;/td&gt;\n"
        if (
    $a == 1) {
        
    $page_content .= "$img_tag\n";
        }
        if (
    $a == ) {
            
    $page_content .= "$img_tag&lt;/tr&gt;&lt;tr&gt;\n";
        } else {
            
    $page_content .= "$img_tag";
        }
    }
    echo 
    $page_content
    And I now have the required </tr><tr> after every three entries.

    The only real issue that remains is the doubleing of the first image.. the output looks like this:

    HTML Code:
    <td align="center"><img src="http://www.autobase.com/photos/00320/1057/10570164_001.jpg" width="325" height="250" /></td>
    <td align="center"><img src="http://www.autobase.com/photos/00320/1057/10570164_001.jpg" width="325" height="250" /></td>
    <td align="center"><img src="http://www.autobase.com/photos/00320/1057/10570164_002.jpg" width="325" height="250" /></td>
    Still looking....
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  7. #6
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Might be missing something, but why not just take out this part?:

    PHP Code:
        if ($a == 1) {
        
    $page_content .= "$img_tag\n";
        } 
    I don't see what you're doing differently when $a = 1, as opposed to any other number that isn't divisible by 3.

  8. The Following User Says Thank You to Schmoopy For This Useful Post:

    BLiZZaRD (03-11-2010)

  9. #7
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    That did it! Neat.

    Now... to figure out the new line after each... hmmm I will ponder this later.

    Thanks again Schmoopy
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  10. #8
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Ah forgot to take that bit out.

    You don't need to use entities in your code to get the html output.

    you can use htmlspecialchars(); http://php.net/manual/en/function.htmlspecialchars.php.

    New line after each <br />? or

    at the start before the for loop make page_content = "<tr><td>"

    then make

    if ($a % 3 == 0 ) {
    $page_content .= "$img_tag</td>\n</tr>\n<tr>\n<td>";
    Corrections to my coding/thoughts welcome.

  11. #9
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    No, I want the entities, LOL.. that part is cool.

    The new line should be after every img_tag but, again, it's not that important.

    Thanks though!
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  12. #10
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,486
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    It's all done. Just wanted to share what you helped with!

    Found here is an example of one of my Craigs List posts. As you can see, there is a lot going on, tons of pictures and such. As you can assume, as well, there is a lot of changes that need to be made to each post, as each car has different information, img URIs and so on and so forth. To add a new post took me about 10 minutes doing everything by hand with my template.

    This link is the final form that I made. With this, I enter the information in the form, click submit and the template code is spit out, all I have to do is copy and paste. Now a new listing takes me about 3 minutes. When I deal with over 100 cars a day, that 7 minutes adds up!

    Anyway, I am sure there is room for improvement, but as my first complete PHP script, I think I did very well. And if not for having to add the </tr><tr> after every 3rd image, I would have been able to do it without any help. But I don't mind asking for help.

    Thanks again you two! Very helpful!
    {CWoT - Riddle } {OSTU - Psycho} {Invasion - Team}
    Follow Me on Twitter: @Negative_Chaos
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

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
  •