473,395 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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,$picsplits) != 0) {
$newText .= $para . "<br /><br />";
} else {
//$newText .= $para . fmod($i,$picsplits) . '<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(",",$phot);[/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?
Apr 7 '07 #1
5 1323
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,$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?
Apr 7 '07 #2
ronverdonk
4,258 Expert 4TB
Post code within php or code tags!

moderator
Apr 7 '07 #3
Sorry 'bout that.. I'll remember next time..
Apr 7 '07 #4
Anyone? Please?
Apr 8 '07 #5
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...
Apr 8 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Mat | last post by:
Hi, I have a system that uploads images as BLOBs in a database. I also have a function that I use to resize uploaded images before saving as files. I would like to combine these two by resising...
1
by: Daniel | last post by:
I have looked everywhere on the web for an answer to this and the only thing I can find is converting the image format when the file is present on the local filesystem. What I want to do is use a...
3
by: Peter Strřiman | last post by:
Hi. I have a web application that needs to run tasks at regular intervals. E.g. it must send out emails every night to people who subscribe to that service. I have come up with one solution,...
6
by: Ivan Weiss | last post by:
Another question for the whizzes out there. I have the following code in a database class: Public Sub insertData(ByVal argInsertSql As String, ByVal argTable As String) Dim myConnection As New...
4
by: IkBenHet | last post by:
Hello, I am working on an ASP.NET based website project. Without going into much detail; One of the function on this website will be the possibility to upload images via a ASP.NET form to the...
6
ak1dnar
by: ak1dnar | last post by:
Hi, I have created Sub in VB.net application, Which reads a text file contents. Sub IPsetter() Dim fileName As String Dim realIParray As String() = Nothing Dim...
2
by: aeblank | last post by:
THE PROBLEM I'm running into performance issues generating and storing a randomly created graph in a SQL Server database. I have a T-SQL script that generates a graph, and then randomly connects the...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.