Log in

View Full Version : Resolved PHP loop help



BLiZZaRD
03-10-2010, 11:05 PM
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 (http://outsidetheurl.com/testing/trialRun.php)

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?

bluewalrus
03-11-2010, 01:52 AM
Something like this?


$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 % 3 ==0 ) {
$page_content .= "</td>\n<td>$img_tag";
} else {
$page_content .= "$img_tag";
}
}

BLiZZaRD
03-11-2010, 02:16 AM
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!

BLiZZaRD
03-11-2010, 09:27 PM
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:



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 % 3 == 0 ) {
$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:



<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?

BLiZZaRD
03-11-2010, 09:50 PM
Apologies for the triple post...

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



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 % 3 == 0 ) {
$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:



<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....

Schmoopy
03-11-2010, 10:01 PM
Might be missing something, but why not just take out this part?:


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.

BLiZZaRD
03-11-2010, 10:09 PM
That did it! Neat.

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

Thanks again Schmoopy

bluewalrus
03-11-2010, 11:26 PM
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>";

BLiZZaRD
03-11-2010, 11:35 PM
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!

BLiZZaRD
03-13-2010, 02:02 AM
It's all done. Just wanted to share what you helped with!

Found here (http://seattle.craigslist.org/skc/ctd/1640117909.html) 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 (http://outsidetheurl.com/testing/CLPost.php) 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!