I want PHP to display 4 alternatives in my table...

A

Anonymous

Guest
I finally have a flatfile system (Thanks to Jay), and it's all sorted and organized by product number like this:

|productnr|name|description|LongDescription||||||

Exaple:

|234|Fish|Cod|Wery good cod from Norway|
|43|Fish|Salmon||
|9|Boat|Speed boat|Fast speed boat, not good for fishing|
|12|Mercury|40hp||

And I have a folder with images, and there is one possible image for each product named with the number of the product:

/images
234.jpg
43.jpg

In the table where I dispaly the products I want php to detect wich icon.gif to wiew...

At $productnuber 234 I have both $LongDescription, and 234.jpg, so I want PHP to display the Picture and text icon like:
<a href='details'><img src='PictTextIcon.gif'></a>

At $productnuber 43 I have no $LongDescription, but i have 234.jpg, so I want PHP to display the Picture icon like:
<a href='details'><img src='Pict.gif'></a>

At $productnuber 9 I have $LongDescription, but no image, so I want PHP to display the text icon like:
<a href='details'><img src='Text.gif'></a>

At $productnumber 12 I have no LongDescription or image, so I want PHP to only display:
 


I have been locking trough diferent cripts but I gave up... I got confused. But I think it's somting like:

if ( :?: ($icon)){
print "<a href='details'><img src='PictTextIcon.gif'></a>";
}
if ( :?: ($icon)){
print "<a href='details'><img src='Pict.gif'></a>";
}
if ( :?: ($icon)){
print "<a href='details'><img src='Text.gif'></a>";
}
if ( :?: ($icon)){
print " ";
}

:lol: :lol: ...... Hey! Stop laughing!!! :? It's not fun! I know I'm a NEWBEE :oops:.

"Take a brake, place give this newbe 2 minutes of your life ...and a dime for a cup of "coffee":roll:"

;]-
 
While you're looping through your array, you can check if you've got a long description (either by checking the length of the array after you explode the line, or if you've set it as a variable, check the length or use the empty() function). So now you know if you've got a description. You can then check whether the file exists by using the is_file() function.

Once you have gotten these you can use an if statement:

if ($desc and $pic) {
do this;
} else if ($desc) {
do this;
} else if ($pic) {
do this;
} else {
do this;
}

Done ;)
 
Thanks again Jay!

It's worcking fine. But I'm loocking forward to get some critical eyes on it...
I will make the "nasty" details availeble when I have got some sleep...

;]-
 
<?php
print "<TABLE... </TR>\n";
$contents = file("file.txt");
while(list($num,$line) = each($contents)) {
list(,$prn, $name, $descr, $ldescr) = explode("|",$line);
$dirB = "image folder/";
$extB = ".jpg";
$dirPD = "detail folder/";
$extPD = ".php";
$icon = "$dirB$prn$extB";
if ((file_exists($icon)) and $ldescr) {
print "<TR VALIGN=TOP><TD WIDTH=10><a href='$dirPD$prn$extPD'><img src='icon folder/picture_text.gif' border='0' alt='Bilde og Informasjon til $name'></a>";
} else if (file_exists($icon)) {
print "<TR VALIGN=TOP><TD WIDTH=10><a href='$dirPD$prn$extPD'><img src='icon folder/picture.gif' border='0' alt='Bilde av $name'></a>";
} else if ($ldescr) {
print "<TR VALIGN=TOP><TD WIDTH=10><a href='$dirPD$prn$extPD'><img src='icon folder/txt.gif' border='0' alt='Informasjon til $name'></a>";
} else {
print "<TR VALIGN=TOP><TD WIDTH=10> ";
}
print "
</TD>
<TD WIDTH=40 BGCOLOR='#FFFFCC' align='right'> $prn
</TD>
<TD width=50> $name
</TD>
<TD> $descr
</TD>
<TD WIDTH=40 BGCOLOR='#CCFFFF' align='right'> $ant
</TD>
</TR>
\n";
}

print "</TBODY></TABLE>\n";
?>

Is that okay enough?
 
Couple of things.

Move the
$dirB = "image folder/";
$extB = ".jpg";
$dirPD = "detail folder/";
$extPD = ".php";
$icon = "$dirB$prn$extB";
out of the loop. Otherwise they'll be declared on every loop which wastes cycles.
Also, $icon should be $dirB.$prn.$extB, its much neater and syntacically correct.

You can also put <TR VALIGN=TOP><TD WIDTH=10> outside the if-else structure because it always gets printed (as you've done with the </td>).

Use:
print "<a href='".$dirPD.$prn.$extPD."'>"; instead of
print "<a href='$dirPD$prn$extPD'>";
 
Couple of things.

Move the
$dirB = "image folder/";
$extB = ".jpg";
$dirPD = "detail folder/";
$extPD = ".php";
$icon = "$dirB$prn$extB";
out of the loop. Otherwise they'll be declared on every loop which wastes cycles.
Also, $icon should be $dirB.$prn.$extB, its much neater and syntacically correct.

You can also put <TR VALIGN=TOP><TD WIDTH=10> outside the if-else structure because it always gets printed (as you've done with the </td>).

Use:
print "<a href='".$dirPD.$prn.$extPD."'>"; instead of
print "<a href='$dirPD$prn$extPD'>";

The only things that worcked from your suggestions was the $abc.$abc.$abc changes...

The rest messed up the script.

:? sorry
 
Move the
$dirB = "image folder/";
$extB = ".jpg";
$dirPD = "detail folder/";
$extPD = ".php";
$icon = "$dirB$prn$extB";
out of the loop. Otherwise they'll be declared on every loop which wastes cycles.

How do I bring it out of the loop, whithout losing the information in the loop.

Im now working on some other part of the script with several "function()"
and allso there I need a way to get data into the function()s without repeating all the $.. over and over again for each "function()"

I tried different ways, but I just dont know how.
 
Check
www.php-forums.com/forums/showthread.php?s=&postid=394
No more Q! ;]-
 
Back
Top