Jump to content

PHP problem with looping & reference to db - insert pics at intervals in text


Dionysus

Recommended Posts

Hi everyone - this is my first post on here and I'm kinda freaking out cause this code is just not working.  I hope some of you can help me get past this so I can catch up to the rest of my work!

 

Thanks!

 

I have a page that displays an article from a table named articles -- clever, huh? :) -- and the relevant fields are text, photo1, photo2, photo3 etc (up to 6).

 

What I'm doing is breaking up the text by line breaks into an array and then printing the items but after an interval of 3 line breaks, an image is inserted from the fields photo2, photo3 etc as long as the photo field is not empty.  The first photo field is auto-entered in the HTML so is skipped for this code.

 

Now everything is going OK except that for some reason, the reference to the photo fields are ignored and nothing is printed.

 

I've tried the following code as well as another version where the array of photo fields is created outside this loop, however, either way, the call to the db does not work from within the loop for some reason.

 

---------------------------------------------------------------------

 

$len = count($revText);

$picsplits = 3;

$pnum = 1;

 

for ($i = 0; $i < $len; ++$i) { //start for loop

 

$pcol = "photo" . $pnum; // fields are called photo1, photo2 etc

 

if (fmod($i,$picsplits) != 0) { //start checking for intervals

$newText .= $revText[$i] . "<br /><br />"; //add regular lines with no image

} else {

 

//define maybepic or if blank then nothing gets printed - so see if photos exist to insert

 

if ($pnum > 1) { // do not print first pic - already done in html

 

if (($i > 2) && (strlen($row_rsArticle[$pcol]) > 1)) {

$maybepic = "<img src='images/" . $row_rsArticle[$pcol] . "' name='" . $pcol . "' border='0' alt='" . $pcol . "'>";

} else {

//$maybepic = "<strong>----NOT WORKING - " . $pcol . "-----</strong>";

$maybepic = "<strong>" . $row_rsArticle[$pcol] . "</strong>";

}

}

 

$newText .= $revText[$i] . $maybepic . "<br /><br />"; // add lines with images IF exist

$pnum++;

 

} // end if mod = 0

} // end for loop

 

----------------------------------------------------------------

 

As you can see, I have commented out the first line of defining $maybepic - this line, when active, did in fact print ---photo1---, ---photo2--- etc at the right intervals.

 

However, when I try it with the "real thing" - $row_rsArticle[$pcol] - nothing shows up. How come I can't refer to a field from within the loop?

I realize that some of the code was confused.. so here it is again within the code tags, however this is the version that is using the array of photo1, photo2, etc..  that was created outside the loop and then referenced inside it.

 

Here's the first part creating the photo loop:

 

// create photo array to reference later
$pnum1 = 2;
$phot = "";

while ($pnum1 < 7) {
$pcol = "photo" . $pnum1;
if ($row_rsArticle[$pcol] != "") {
$phot .= '"' . $row_rsArticle[$pcol] . '",';
}
$pnum1++;
}
$photolist = explode(",",$phot);
$lenpics = count($photolist);

 

And here's the part after that calls it (unsuccessfullyl still):

 

						$len = count($revText);
		$picsplits = 3;
		$pnum = 1;

		for ($i = 0; $i < $len; ++$i) { //start for loop

		$pcol = "photo" . $pnum; // fields are called photo1, photo2 etc

			if (fmod($i,$picsplits) != 0) {  //start checking for intervals
			$newText .= $revText[$i] . "<br /><br />";  //add regular lines with no image
			} else {

			//define maybepic or if blank then nothing gets printed - so see if photos exist to insert

				if ($pnum > 1) { // do not print first pic - already done in html

					if (($i > 2) && ($pnum <= $lenpics)) {

					$maybepic = "<img src='images/" . $photolist[($pnum-1)] . "' name='" . $pcol . "' border='0' alt='" . $pcol . "' width=200 class='imgcontourbleu'>";

					} else {
					$maybepic = "NO PHOTO " . $pcol ; // this is to show error but will disappear after
					}

					}

			$newText .= $revText[$i] . $maybepic . "<br /><br />"; // add lines with images IF exist
			$pnum++;


			} // end if mod = 0
		} // end for loop     
     

 

 

OK.. I figured it out myself.. I created the image array outside the loop, however, I called the function that cuts up the text and threw in the loop count and loop contents when doing so... like this:

 

echo articleFormat($row_rsArticle['ftext'],$phot,$lenpics);

 

where $lenpics is the length of the array and $phot is the string before exploding the array..

 

This way, I tried to inserted it into the function and it worked. I don't know if this is how it's supposed to be done.. but all I know is that it works.

 

Phew...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.