I have this bit of code, that isn't valid, and I need some help getting
it to work... Currently, I get an error because I am calling the next()
function within a foreach loop. I am opening a flat file with data in
the format of:
01|data1|data2
01 Being Gallery Name, data1 being the filename, data2 being photo caption.
Basically I need to look at the line from the text file, and if the
first 2 digits do not match the gallery I have specified, then move on
to the next line, completely ignoring the first line... Code follows:
I would appreciate any help...
<?
$gallery = 03;
$data = file('gallery.txt');
echo "<table border=0 width=500 align=center>";
foreach ($data as $line)
{
list ($g, $q, $a) = explode('|' , trim($line) );
if ( $g != $gallery ) next($line);
else
{
echo "<td width=50% valign=top><img src='/gallery/$q' width=220><br>";
echo "$a<br>";
echo "<a href=\"/gallery/$q\">Enlarge</a><br><br></td>";
$ee++;
//2 - row
if($ee%2==0) echo "<tr>";
}
echo "</table>";
}
?> 13 5183
matt wrote: I have this bit of code, that isn't valid, and I need some help getting it to work... Currently, I get an error because I am calling the next() function within a foreach loop. I am opening a flat file with data in the format of:
01|data1|data2
01 Being Gallery Name, data1 being the filename, data2 being photo caption.
Basically I need to look at the line from the text file, and if the first 2 digits do not match the gallery I have specified, then move on to the next line, completely ignoring the first line... Code follows:
I would appreciate any help...
<?
$gallery = 03;
$data = file('gallery.txt');
OK, you now have an array called $data with all lines. echo "<table border=0 width=500 align=center>"; foreach ($data as $line) { list ($g, $q, $a) = explode('|' , trim($line) ); if ( $g != $gallery ) next($line); else {
Why do you test for something you do not care about?
And what is next() in this context?
It is wrong.
Try this:
if ($g == $gallery ){
// print out here
}
echo "<td width=50% valign=top><img src='/gallery/$q' width=220><br>"; echo "$a<br>"; echo "<a href=\"/gallery/$q\">Enlarge</a><br><br></td>"; $ee++; //2 - row if($ee%2==0) echo "<tr>"; } echo "</table>";
}
?>
regards,
Erwin Moller I have this bit of code, that isn't valid, and I need some help getting it to work... Currently, I get an error because I am calling the next() function within a foreach loop. I am opening a flat file with data in the format of:
01|data1|data2
<?
$gallery = 03;
$data = file('gallery.txt');
echo "<table border=0 width=500 align=center>"; foreach ($data as $line) { list ($g, $q, $a) = explode('|' , trim($line) ); if ( $g != $gallery ) next($line); else { echo "<td width=50% valign=top><img src='/gallery/$q' width=220><br>"; echo "$a<br>"; echo "<a href=\"/gallery/$q\">Enlarge</a><br><br></td>"; $ee++; //2 - row if($ee%2==0) echo "<tr>"; } echo "</table>";
}
?>
Don't mix foreach() and next(). Change the test around to avoid it, and
use break to quit the test after a successful match (make sure that
your data is sorted by gallery).
foreach ($data as $line)
{
// ...
if( $g == $gallery )
{
// ...do the table output here...
// then get out of the loop:
break;
}
}
// this should be outside the foreach() loop:
echo '</table>';
---
Steve
On Mon, 12 Dec 2005 23:03:51 +1000, matt wrote:
The line if ( $g != $gallery ) next($line);
should be replaced by
if ( $g != $gallery ) continue;
-- http://www.mgogala.com The line if ( $g != $gallery ) next($line);
should be replaced by
if ( $g != $gallery ) continue;
After the correct gallery is found this solution will waste cycles
looking at entries that will not match.
---
Steve if ($g == $gallery ){ // print out here }
After the correct gallery is found this solution will waste cycles
looking at entries that will not match.
---
Steve
Steve wrote: if ($g == $gallery ){ // print out here } After the correct gallery is found this solution will waste cycles looking at entries that will not match.
If every galery is only mentioned only once in the file: Yes.
But I don't have that arcane knowledge.
Regards,
Erwin Moller --- Steve After the correct gallery is found this solution will waste cycles looking at entries that will not match.
If every galery is only mentioned only once in the file: Yes. But I don't have that arcane knowledge.
Oops. I'll shut up now.
---
Steve
Steve wrote: The line
if ( $g != $gallery ) next($line);
should be replaced by
if ( $g != $gallery ) continue;
After the correct gallery is found this solution will waste cycles looking at entries that will not match.
--- Steve
Thanks for all the responses.
As it is a flat file that is used for all galleries, and it appends a
line to the bottom of the file each time a record is added, the
galleries are mixed up throughout the file, so if it stops as soon as
the first match is found, it will not print the rest of the records out.
I will try the "continue" method and see how it works.
matt wrote: Steve wrote:
The line if ( $g != $gallery ) next($line);
should be replaced by
if ( $g != $gallery ) continue; After the correct gallery is found this solution will waste cycles looking at entries that will not match.
--- Steve
Thanks for all the responses.
As it is a flat file that is used for all galleries, and it appends a line to the bottom of the file each time a record is added, the galleries are mixed up throughout the file, so if it stops as soon as the first match is found, it will not print the rest of the records out.
I will try the "continue" method and see how it works.
The Continue Method is causing me problems, After the first instance
where $g != $gallery, it goes to the end of the code and closes the
table... then it goes back and prints the second photo, causing the
second image to not be inside the table that is opened at the start of
the code (I have pasted complete html result at the bottom:)
<table border=1 width=500 align=center><td width=50% valign=top><img
src='photo1.jpg' width=220><br>Test<br><a
href="photo1.jpg">Enlarge</a><br><br></td></table><td width=50%
valign=top><img src='photo2.jpg' width=220><br>Test2<br><a
href="photo2.jpg">Enlarge</a><br><br></td><tr></table> The Continue Method is causing me problems, After the first instance where $g != $gallery, it goes to the end of the code and closes the table... then it goes back and prints the second photo, causing the second image to not be inside the table that is opened at the start of the code
Firstly, you make the other correction suggested, moving the close
table tag outside the loop? And secondly, use Erwin's logic which does
not require an explict "continue":
foreach ($data as $line)
{
// ...
if( $g == $gallery )
{
// ...do the table output here...
}
}
// this should be outside the foreach() loop:
echo '</table>';
---
Steve
Steve wrote: The Continue Method is causing me problems, After the first instance where $g != $gallery, it goes to the end of the code and closes the table... then it goes back and prints the second photo, causing the second image to not be inside the table that is opened at the start of the code
Firstly, you make the other correction suggested, moving the close table tag outside the loop? And secondly, use Erwin's logic which does not require an explict "continue":
foreach ($data as $line) { // ... if( $g == $gallery ) { // ...do the table output here... } } // this should be outside the foreach() loop: echo '</table>';
--- Steve
Worked a treat. Thanks for your help!
On Tue, 13 Dec 2005 06:45:38 -0800, Steve wrote: The Continue Method is causing me problems, After the first instance where $g != $gallery, it goes to the end of the code and closes the table... then it goes back and prints the second photo, causing the second image to not be inside the table that is opened at the start of the code Firstly, you make the other correction suggested, moving the close table tag outside the loop? And secondly, use Erwin's logic which does not require an explict "continue":
foreach ($data as $line) { // ... if( $g == $gallery ) { // ...do the table output here... } } // this should be outside the foreach() loop: echo '</table>';
Yup, I agree with this. That would solve the problem. --- Steve
-- http://www.mgogala.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Randell D. |
last post by:
Folks,
Ever since reading an interesting article in Linux Format on PHP whereby
suggested code writing was made that could enhance performance on a server,
I've started testing various bits of...
|
by: James Curran |
last post by:
I'd like to make the following proposal for a new feature for the C#
language. I have no connection with the C# team at Microsoft. I'm posting
it here to gather input to refine it, in an "open...
|
by: Hans |
last post by:
I implemented the IEnumerator interface in some classes to enable the use of the foreach statement. As you probalbly know, the interface asks for the implementation of
object IEnumerator.Curren...
|
by: cody |
last post by:
What about an enhancement of foreach loops which allows a syntax like that:
foeach(int i in 1..10) { } // forward
foeach(int i in 99..2) { } // backwards
foeach(char c in 'a'..'z') { } // chars...
|
by: Josh Ferguson |
last post by:
I don't believe a syntax driven equivalent exists for this, but I just
thought it would be neat if you could use foreach to do something like this:
foreach (Object x in collection1, collection2)...
|
by: TrintCSD |
last post by:
How can I reset the collections within a foreach to be read as a change from
within the foreach loop then restart the foreach after collections has been
changed?
foreach(string invoice in...
|
by: Nathan Sokalski |
last post by:
I am trying to use the System.Array.ForEach method in VB.NET. The action
that I want to perform on each of the Array values is:
Private Function AddQuotes(ByVal value As String) As String
Return...
|
by: greyseal96 |
last post by:
Hi,
I am a pretty new programmer, so I apologize in andvance if this is a dumb question...
In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
|
by: mattehz |
last post by:
Hey there,
I am trying to upload old source files and came across these errors:
Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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...
|
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,...
| |