PHP trouble with inserting images at regular intervals throughout text from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dionysus
    New Member
    • Apr 2007
    • 5

    PHP trouble with inserting images at regular intervals throughout text from database

    Using (or massacring) PHP, I'm trying to read the contents of a text field in a database (magazine article) and then break it into lines and insert an image file ever 3-4 lines if the image is specified (lower down).

    The code I'm using to do this is the following:

    [PHP]
    $picnum = 0;
    $picsplits = 3;
    $i=0;

    foreach ($revText as $para) {

    if (fmod($i,$picsp lits) != 0) {
    $newText .= $para . "<br /><br />";
    } else {
    //$newText .= $para . fmod($i,$picspl its) . '<br />';
    $newText .= $para . $photos[$picnum] . '*<br /><br />';
    $picnum++;
    }
    $i++;
    }
    [/PHP]

    Each entry also contains 6 fields called photo1 through photo6 which can be filled in starting from the first up until as many photos are needed for the article. I've managed to get these into an array using this code:

    [PHP]
    $pnum = 2;
    $phot = "";
    while ($pnum < 7) {
    $pcol = "photo" . $pnum;

    if ($row_rsArticle[$pcol] != "") {
    $phot .= '"' . $row_rsArticle[$pcol] . '",';
    }

    $pnum++;
    }

    $photos = explode(",",$ph ot);[/PHP]

    Now I would think that the array $photos is defined in this code so that I should be able to refer to it later on in the bit of code at the beginning of this message which adds the image to the output. However, this is sadly not happening.

    I realize I have to still add the <img src etc.. but I think that is the easier part. I also did in fact print out $photos and it IS indeed OK and prints fine when not used in the function for outputting the text and images.

    It's just inside the foreach loop that it's not working...

    Help please?
  • Dionysus
    New Member
    • Apr 2007
    • 5

    #2
    OK.. I've tried rewriting the code using a for loop instead and trying to do everything in the same loop.

    I have the following code now:
    ---------------------------------------------------------------------

    $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,$picsp lits) != 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_rs Article[$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?

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Post code within php or code tags!

      moderator

      Comment

      • Dionysus
        New Member
        • Apr 2007
        • 5

        #4
        Sorry 'bout that.. I'll remember next time..

        Comment

        • Dionysus
          New Member
          • Apr 2007
          • 5

          #5
          Anyone? Please?

          Comment

          • Dionysus
            New Member
            • Apr 2007
            • 5

            #6
            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:

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

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

            Comment

            Working...