Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

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

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

Postby Pinex » Fri Nov 01, 2002 12:44 pm

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:
&nbsp;


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 "&nbsp;";
}

: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:"

;]-
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby Jay » Fri Nov 01, 2002 2:01 pm

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 ;)
Jay
 

It works.....

Postby Pinex » Sun Nov 03, 2002 7:21 pm

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

;]-
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

This is what i did..

Postby Pinex » Mon Nov 04, 2002 5:13 am

<?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>&nbsp;";
}
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?
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Postby Jay » Mon Nov 04, 2002 7:19 am

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'>";
Jay
 

Sorry Jay...

Postby Pinex » Thu Nov 14, 2002 1:33 pm

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
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

Simple question I guess.

Postby Pinex » Sat Nov 16, 2002 11:15 am

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.
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW

I got hlp!

Postby Pinex » Sat Nov 16, 2002 2:29 pm

Check
www.php-forum.com/forums/showthread.php?s=&postid=394
No more Q! ;]-
User avatar
Pinex
New php-forum User
New php-forum User
 
Posts: 23
Joined: Tue Oct 29, 2002 5:06 am
Location: Norway & TheWWW


Return to PHP coding => General

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron