PDA

View Full Version : What's wrong with this code? [php] +reps



Blinger
27-06-2012, 12:25 AM
Hey everyone.
I'm having problems with my scrappy code. It only serves as a purpose to show me the recipes that I have acquired.. Only problem is it is showing just the first ingredient.. Doesn't even show me the name of the recipe??

Here's the code I have written up:



<?php
$recipe = "Coffee and Date Dough"; // this isn't normally hard coded..
echo('<table width="25%" border="1">');

// This basically gets all of the recipe into an array
$query = mysql_query("SELECT * FROM recipe WHERE recipe='{$recipe}'");
$count = 1;
while($row = mysql_fetch_array($query)){
// Couldn't think of a better way to count to 18 without using something like this.
$ing = "ing".$count;
$per = "per".$count;

// Echo out the table information
echo("<tr><td>$row[$ing]</td><td>$row[$per]</td></tr>");
$count++;
}
echo("</table>");
?>


and here is a sample sql


CREATE TABLE IF NOT EXISTS `recipe` (
`recipe` text NOT NULL,
`ing1` text NOT NULL,
`per1` float NOT NULL,
`ing2` text NOT NULL,
`per2` float NOT NULL,
`ing3` text NOT NULL,
`per3` float NOT NULL,
`ing4` text NOT NULL,
`per4` float NOT NULL,
`ing5` text NOT NULL,
`per5` float NOT NULL,
`ing6` text NOT NULL,
`per6` float NOT NULL,
`ing7` text NOT NULL,
`per7` float NOT NULL,
`ing8` text NOT NULL,
`per8` float NOT NULL,
`ing9` text NOT NULL,
`per9` float NOT NULL,
`ing10` text NOT NULL,
`per10` float NOT NULL,
`ing11` text NOT NULL,
`per11` float NOT NULL,
`ing12` text NOT NULL,
`per12` float NOT NULL,
`ing13` text NOT NULL,
`per13` float NOT NULL,
`ing14` text NOT NULL,
`per14` float NOT NULL,
`ing15` text NOT NULL,
`per15` float NOT NULL,
`ing16` text NOT NULL,
`per16` float NOT NULL,
`ing17` text NOT NULL,
`per17` float NOT NULL,
`ing18` text NOT NULL,
`per18` float NOT NULL,
`eyf` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `recipe`
--

INSERT INTO `recipe` (`recipe`, `ing1`, `per1`, `ing2`, `per2`, `ing3`, `per3`, `ing4`, `per4`, `ing5`, `per5`, `ing6`, `per6`, `ing7`, `per7`, `ing8`, `per8`, `ing9`, `per9`, `ing10`, `per10`, `ing11`, `per11`, `ing12`, `per12`, `ing13`, `per13`, `ing14`, `per14`, `ing15`, `per15`, `ing16`, `per16`, `ing17`, `per17`, `ing18`, `per18`, `eyf`) VALUES
('Coffee and Date Dough', 'Bakers Flour', 100, 'Salt', 2, 'Improver', 1, 'Gluten', 6, 'Coffee (Kularome)', 6, 'Yeast', 6, 'Water (approximately)', 60, 'Sultanas (Conditioned)', 20, 'Dates (Inspected)', 50, 'Total Weight:', 0, 'E.Y.F.', 2.51, 'Notes:', 0, 'Hey!', 0, '', 0, '', 0, '', 0, '', 0, '', 0, '');


@robbie! ?

Chippiewill
27-06-2012, 12:40 AM
I think that a serialised array would have been a better option that 20 columns..

Also I think it only iterates the while loop once as once you call mysql_fetch_array($query) it moves to the next mysql row of results, since theres' only one row after the first call of the function it's equal to 0 which terminates the loop.

Blinger
27-06-2012, 12:49 AM
ah okay. I'm a bit confuzzled now. How would I go about making it show in a table? It's mainly for use on mobile phones (since if I'm at work it'd be easiest to look at it there)..

I mean, I don't REALLY want to have to write heaps of code just to spit all the information out but if i have to, i have to...

HotelUser
27-06-2012, 01:03 AM
Without looking to see if your mysql methods are being called correctly and without looking at your sql table at all, immediately I can see that the reason (at least) that you're only being displayed the first bit of data is because you never actually increment $count in the correct place. You will need to do (something) like this:

http://pastebin.com/kQSGAZJb

For every iteration of your while loop, you're iterating through the different rows of your sql table, but I'm just going to assume that every row has a variable called ing1, ing2, ing3 and per1, per2 and per3 etc etc. so you could either write out a bunch of lines to accommodate for each of these variables inside your while loop, or you could use a for loop (something similar as above to suit your needs), to build the keys for your associative array and print out their values accordingly inside that while loop.

Trigs
27-06-2012, 01:13 AM
Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop

Blinger
27-06-2012, 01:17 AM
Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop

Because I grabbed the ingredients from another file which doesn't display in firefox/chrome, only IE. Might do it your way. Unless someone knows how/why a sample file like this doesn't work? (worst.coding.everrrrrr!)

http://pastebin.com/KsDuMScn

Chippiewill
27-06-2012, 08:52 AM
Why do you store each ingredient in it's own row resulting in several unecessary rows? Why not create one ingredients row and store them separated by some character? Ex: "1 cup milk|2 cups butter|1 cup flour". Then when you need to display them, use implode() and a loop
He should be serialising an array to do this instead. For readability he should serialise them with JSON.

json_encode() before they go into the array and json_decode() as they come out.

Also I believe with your method you need to explode() the string not implode().

Edit:

Last edited by HotelUser; Today at 02:07 AM. Reason: put source on pastebin because fckeditor is horrible
Disable the rich text editor in the control panel, it's not too great for any kind of use.

HotelUser
27-06-2012, 02:12 PM
He should be serialising an array to do this instead. For readability he should serialise them with JSON.

json_encode() before they go into the array and json_decode() as they come out.

Also I believe with your method you need to explode() the string not implode().

Edit:

Disable the rich text editor in the control panel, it's not too great for any kind of use.

The rich text editor also doesn't seem to play nice with the iPad either, as aforementioned vBulletin's implementation of CKeditor is horrifying :P

The OP could have taken a serialized approach here, or as someone else mentioned just used CSV using a delimiter of | (using implode to store and explode to display), although that's not the immediate bug in his code causing it to not function. Regardless of his technique in storing data serialized or largely in SQL, his error was in how he iterated through his data per row of content. Perhaps once the OP has validated that his current model is functional, he could read up on CSV and serialization methods of PHP, because I know at least the latter is well documented on the PHP reference manual :)

Blinger
27-06-2012, 04:04 PM
The rich text editor also doesn't seem to play nice with the iPad either, as aforementioned vBulletin's implementation of CKeditor is horrifying :P

The OP could have taken a serialized approach here, or as someone else mentioned just used CSV using a delimiter of | (using implode to store and explode to display), although that's not the immediate bug in his code causing it to not function. Regardless of his technique in storing data serialized or largely in SQL, his error was in how he iterated through his data per row of content. Perhaps once the OP has validated that his current model is functional, he could read up on CSV and serialization methods of PHP, because I know at least the latter is well documented on the PHP reference manual :)

I just copied your method. If I ever get time I'll suss that stuff out. I've been busy/tired from work recently though!

Want to hide these adverts? Register an account for free!