473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with values on a foreach loop and how can I assign the correct value

2 New Member
Hi! I have a blog, and I am creating a script to show my posts (title-content-image) in another page. With the code below I get the first image of the post inside of `<div class="entry">`

The problem is : Imagine that I have 10 posts. Each of these has 1 title 1 content and images. Some of my posts do not have images at all. For example, the title of post 4 will be saved to variable "vartitles4", the content to "varcontents4" and the image to "varimages4". If post 4 does not have any images, it means that "varimages4" will have the value that was meant to be saved on "varimages5". (so varimages10 value will be null at the end)

So, how can I assign a value like a non-image.gif to varimages4 so that the script will work properly ?

Below is the code that gets the images. I have another two blocks of code like this one (title-content) before that.

Expand|Select|Wrap|Line Numbers
  1.     $img = $xPathimages->query('//div[@class="entry"]//img'); 
  2.     $thefirst = 1;
  3.     $iimg=0;
  4.     foreach ($img as $imgs) { 
  5.     $images = $imgs->getAttribute('src'); 
  6.     ${'varimages' . $iimg} = $images; //the value is saved to varimages1/2/3/4/5/6...
  7.     if($thefirst == 1) break; //gets only the first image
  8.     $iimg=$iimg+1;
  9.     }
Thank you people!
Mar 28 '11 #1
0 935

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

Similar topics

9
by: Art | last post by:
Hello, Here is a fragment of my code, which fails and I don't know why. --------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(@"C:\mydoc.xml"); foreach (XmlNode objNode...
6
by: Dave Spencer | last post by:
Hi all, New to this group and to C# in general (experienced in MFC). Anyway I have a ListView and a foreach loop that is doing something to each item in the list and updating the list on the...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
2
by: Justin | last post by:
I don't use the foreach statement often so this problem has cought me off guard. Here is the code: foreach (int i in EventDates.Items) { DateList += EventDates.Items.ToString(); } I get an...
4
by: Jack E Leonard | last post by:
I'm looping through the keys and values of a form submission using foreach($_POST as $key => $value) echo "$key = $value<br>"; No problems there. All works as expected. But seveal of the...
2
by: radishcarrot | last post by:
unsigned char *m_pVolumeData; m_pVolumeData = new unsigned char; FILE *fp = fopen(filename, "rb"); if(fp) { fread(m_pVolumeData, 1, m_nSizeX * m_nSizeY * m_nSizeZ, fp); fclose(fp); } ...
10
by: fig000 | last post by:
HI, I'm new to generics. I've written a simple class to which I'm passing a generic list. I'm able to pass the list and even pass the type of the list so I can use it to traverse it. It's a...
2
by: somacore | last post by:
VS2005, C# Windows Form I can't quite figure out how to do this, but i think I'm close. I have a groupbox which contains 8 radio buttons, and I need to figure out which one is checked for entry...
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
16
by: Magda Balise | last post by:
So as everyone else I am new to PHP and I have been trying to create an amenity page for a B&B. I need folk to be able to select checkboxes and it fetches the info from the database. I posted the...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.