You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more
I am trying to code a page for a website with a small auction. I've got the page layout and input form for bids sorted but am trying now to sort out the display of bids. I'm not really up to speed on php but I'm working with 'Build your own database driven website, using php & mysql' (only edition 4 from 2009).
I can extract the data from the database using the following:
<html>
<head>
<title>Retrieve data from database </title>
</head>
<body><?php
// Connect to database server
mysql_connect("localhost", "deverono_mikejd", "d4r1us") or die (mysql_error ());// Select database
mysql_select_db("deverono_wb") or die(mysql_error());// SQL query
$strSQL = "SELECT field7,field11 FROM 2007mod_mpform_results_249 WHERE 1";// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {// Write the value of the column FirstName (which is now in the array $row)
echo $row['field7'].$row['field11']."
";}
// Close the database connection
mysql_close();
?>
</body>
</html>
I then try to place it into a table for presentation, but this fails:
<html>
<head>
<title>Retrieve data from database </title>
</head>
<body><?php
// Connect to database server
mysql_connect("localhost", "deverono_mikejd", "d4r1us") or die (mysql_error ());// Select database
mysql_select_db("deverono_wb") or die(mysql_error());// SQL query
$strSQL = "SELECT field7,field11 FROM 2007mod_mpform_results_249 WHERE 1";// Execute the query (the recordset $rs contains the result)
$rs = mysql_query($strSQL);// Loop the recordset $rs
// Each row will be made into an array ($row) using mysql_fetch_array
while($row = mysql_fetch_array($rs)) {
$lots[] = array('lotname' => $row['field7'], 'bid' => $row['field11']);
}exit()
?>
<?php if (isset($lots)); ?>
<table>
<?php foreach ($lots as $lot): ?>
<tr>
<td> <?php htmlout($lot['lotname']); ?></td>
<td> <?php htmlout($lot['bid']); ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?></body>
</html>
I'm hoping it's something simple, just not fully understanding php. So I'm appealing to STW, can anyone suggest where I'm going wrong, please?
cheers,
Mike
Not done PHP for a few years now but that 'exit()' just before your output table looks like it shouldn't be there.
Also if that's your live password and username it might be an idea to edit it out of your post... 😉
1. What happens ? i.e. what HTML do you get generated ?
2. As above, what's with the exit() ?
I think your first mistake is in using PHP. It has to be the ugliest language ever invented that's in actual widespread use.
[quote=oldnpastit said]I think your first mistake is in using PHP. It has to be the ugliest language ever invented that's in actual widespread use.
As with a lot of languages, it's possible to write shocking code or very elegant code.
My first PHP site had the mixed HTML/PHP content as in the OP's example. I then moved on to a template based approach which allowed a degree of separation between the PHP and the presentation HTML. Much cleaner IMO.
I used http://pear.php.net/package/HTML_Template_Flexy there are lots of great PEAR packages available.
OK, I've removed the exit() and tried again. Still doesn't work and the HTML generated is blank.
Thanks for the advice on password but now too late to edit. I missed that.
Shouldn't "<?php if (isset($lots)); ?>"
be "<?php if (isset($lots)): ?>" ?
i.e. colon not semi-colon at the end ?
I'm old skool and use the "normal" syntax and curly braces {} 🙂
It may not be the quickest way to complete this. But I'd try building it up slowly;
Forget about the DB connection and basing the while on the recordset.
Get a loop working that displays an incrementing value or similar just to prove out the loop.
Then get a value out of the DB and write it into the html
Then get a collection of values from the DB and write them all into HTML.
Then merge the loop and the collection.
I'm not a PHP guy so can't offer any technical help with this, sorry!
Replacing the ; with : has had some effect.
HTML generated is now:
<html>
<head>
<title>Retrieve data from database </title>
</head>
<body>
<table>
<tr>
<td>
Where's the htmlout function defined ? What happens if you remove that and just output the values from the array directly ?
I think you're getting errors but your PHP settings are such that you're not seeing the error reports.
If you have control over the php.ini file then have a look at the following with a view to enabling error reporting in your dev environment
http://php.net/manual/en/errorfunc.configuration.php#ini.error-reporting
Or try bunging this at the top of your PHP code file
error_reporting(E_ALL);
ini_set('display_errors', '1');
Removing htmlout obviously does something as the HTML generated is now the full table with 4 rows but no content.
OK, that's good. Tells us that the problem was htmlout [I did mean replace htmlout with a print/echo]
So, what about
<td><?=$lot['lotname']?></td>
<td><?=$lot['bid']?></td>
or if that doesn't work then
<td> <?php print($lot['lotname']); ?></td>
<td> <?php print($lot['bid']); ?></td>
May I suggest you register yourself on a PHP forum such as http://forums.phpfreaks.com/ for help, you'll get a better response.
Cheeky!
Success!! Thanks allthepies. The print() function did it. Hope I haven't taken up too much of your time. Much appreciated.
Do a print_r on the array returned by the query first of all to see if there are any results returned.
Ie.
print_r($rs);
If there's something in it then it's something wrong with your HTML. Otherwise there's something wrong with your query.
Got beaten to it glad you got going.
I've been messing around with PHP / mysql / JS a bit last few weeks I quite like it (low level C programmer by trade).
No probs. You might want to look at replacing print with something that is HTML aware and will escape any characters which could cause HTML issues (which is presumably what the missing htmlout function was looking to achieve).
Is the sql right? Shouldn't it be WHERE some field = 1;?
[quote=tinribz said]Is the sql right? Shouldn't it be WHERE some field = 1;?
It means "where true" and is effectively redundant in this case.