Menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Regicollis
Forum Newbie
Posts: 8
Joined: Thu Nov 25, 2010 8:51 am

Menu

Post by Regicollis »

I'm doing a website for a relative who has a small catering business. A part of the website will be the menus. I would like to load the data for the menus from a .txt file.

I would like each item on the menu to be displayed on two rows. The first row will contain a cell with the name of the dish, an empty cell with 100% width and a dotted border at the bottom and finally a cell displaying the price of the dish. The second row should only contain a single cell with a colspan of 3 and contain a description of the dish. Thus each dish will have four cells. To make things even more complicated I would like to assign a class to each of the four cells so I can style them individually.

You can see an example of what I would like you can go to http://rasmusk.wid.ots.dk/projekter/kog ... retter.php where I have made a menu using static HTML.

I've already found out how to generate a HTML-table from a .txt-file but the way the script works is like this: Each line in the .txt file is divided using a delimiter. The code then takes each item on each line and generates a cell from. The only way I've been able to get reasonably near what I want has been to structure the .txt file like this (using ; as delimiter):

Code: Select all

Name of dish; ;price
Description; ; ;
This does not provide a very desirable result since I'm not able to assign styles to the cells and since I'm not able to make the colspan="3" cell that I need.

I've used the PHP code found here. My code currently looks like this:

Head section

Code: Select all

<?php
$file = "menukort/supper.txt";
$delimiter = "<>";

@$fp = fopen($file, "r") or die("Could not open file for reading");
while (!feof($fp))
	{
    $tmpstr = fgets($fp, 100);
    $line[] = ereg_replace("\r\n", "", $tmpstr);
	}
for ($i=0; $i < count($line); $i++)
	{
    $line[$i] = split($delimiter, $line[$i]);
	}
?>
Body section

Code: Select all

<table class="menukort">
			<?php
				for ($i=0; $i < count($line); $i++)
				{
					echo "<tr>";
					for ($j=0; $j < count($line[$i]); $j++)
					{
						echo "<td>".$line[$i][$j]."</td>";
					}
					echo "</tr>";
				}
				fclose($fp);
			?>
            </table>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Menu

Post by Celauran »

Wouldn't something like this work?

Code: Select all

$fh = fopen('menu.txt', 'r');

while (!feof($fh))
{
    $menu[] = explode(";", fgets($fh, 1024));
}

fclose($fh);

echo "<table>";
foreach ($menu as $dish)
{
    echo "<tr>";
    echo "<td class=\"dish_name\">{$dish[0]}</td>";
    echo "<td class=\"dish_cost\">{$dish[2]}</td>";
    echo "</tr><tr>";
    echo "<td class=\"dish_description\">{$dish[1]}</td>";
    echo "</tr>";
}
echo "</table>";
Regicollis
Forum Newbie
Posts: 8
Joined: Thu Nov 25, 2010 8:51 am

Re: Menu

Post by Regicollis »

Thank you - it is a great improvement over what I had before.

I do however still have a problem. The first line, containing name and price is perfect but the description of the dish gets inserted in the wrong place.

As far as I am able to read the code, the value of $dish_description should be placed in the cell with colspan=3, however the description somehow ends up in a new row. You can see the problem on http://rasmusk.wid.ots.dk/projekter/kog ... retter.php. The description should be in the red cell, which is currently empty.

Here is my code:

Code: Select all

<?php
            $fh = fopen('menukort/supper.txt', 'r');

                while (!feof($fh))
                {
                    $menu[] = explode("<>", fgets($fh, 1024));
                }
                
                fclose($fh);
                
                echo "<table border=\"2\">";
                foreach ($menu as $dish)
                {
                    echo "
						<tr>
						<td class=\"dish_name\">{$dish[0]}</td>
						<td></td>
						<td class=\"dish_cost\">{$dish[2]}</td>
						</tr><tr>
						<td colspan=\"3\" bgcolor=\"red\" class=\"dish_description\">{$dish[1]}</td>
						</tr>";
                }
                echo "</table>";
				?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Menu

Post by Celauran »

That is a little weird. There are more rows in the html than the php would suggest. I've also noticed, however, that you're using '<>' in your explode(). Would you mind posting the contents of supper.txt?
Regicollis
Forum Newbie
Posts: 8
Joined: Thu Nov 25, 2010 8:51 am

Re: Menu

Post by Regicollis »

I changed the <> to * in both the .php and the .txt files but I still get the same result. The supper.txt looks like this:

Code: Select all

Klar Suppe*35,- 
Urter - k&oslash;d- og melboller - ris m. rosiner 
Aspargessuppe*35,- 
Fl&oslash;delegeret med k&oslash;dboller  
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Menu

Post by Celauran »

I see the problem. I was expecting the text file to be in the following format:
Name; Description; Price
Name; Description; Price

Whereas you have:
Name; Price
Description
Name; Price
Description

The easiest solution would be to move description up to the same line as the meal it describes. If the order will be Name; Price; Description, then you'll just need to invert {$dish[1]} and {$dish[2]}

EDIT: If you can't (or don't want to) change the supper.txt file, you could try something like this:

Code: Select all

$fh = fopen('menu.txt', 'r');

while (!feof($fh))
{
    $menu[] = explode("*", fgets($fh, 1024));
}

fclose($fh);

echo "<table>";
for ($i = 0; $i < count($menu); $i++)
{
    echo "<tr>";
    if (($i % 2) == 0)
    {
        echo "<td class=\"dish_name\">{$menu[$i][0]}</td>";
        echo "<td class=\"dish_cost\">{$menu[$i][1]}</td>";
    }
    else
    {
        echo "<td colspan=\"2\" class=\"dish_description\">{$menu[$i][0]}</td>";
    }
    echo "</tr>";
}
echo "</table>";
Regicollis
Forum Newbie
Posts: 8
Joined: Thu Nov 25, 2010 8:51 am

Re: Menu

Post by Regicollis »

It works now - thank you for the help :D
Post Reply