any php experts?
 

  You don't need to be an 'investor' to invest in Singletrack: 6 days left: 95% of target - Find out more

[Closed] any php experts?

18 Posts
8 Users
0 Reactions
46 Views
Posts: 386
Full Member
Topic starter
 

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


 
Posted : 09/03/2015 2:42 pm
Posts: 0
Full Member
 

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


 
Posted : 09/03/2015 2:45 pm
Posts: 0
Free Member
 

1. What happens ? i.e. what HTML do you get generated ?
2. As above, what's with the exit() ?


 
Posted : 09/03/2015 2:49 pm
Posts: 7076
Full Member
 

I think your first mistake is in using PHP. It has to be the ugliest language ever invented that's in actual widespread use.


 
Posted : 09/03/2015 2:49 pm
Posts: 0
Free Member
 

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


 
Posted : 09/03/2015 2:54 pm
Posts: 386
Full Member
Topic starter
 

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.


 
Posted : 09/03/2015 3:09 pm
Posts: 0
Free Member
 

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 {} 🙂


 
Posted : 09/03/2015 3:14 pm
Posts: 5686
Full Member
 

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!


 
Posted : 09/03/2015 3:17 pm
Posts: 386
Full Member
Topic starter
 

Replacing the ; with : has had some effect.

HTML generated is now:

<html>
<head>
<title>Retrieve data from database </title>
</head>
<body>

<table>
<tr>
<td>


 
Posted : 09/03/2015 3:18 pm
Posts: 0
Free Member
 

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


 
Posted : 09/03/2015 3:21 pm
Posts: 386
Full Member
Topic starter
 

Removing htmlout obviously does something as the HTML generated is now the full table with 4 rows but no content.


 
Posted : 09/03/2015 3:26 pm
Posts: 0
Free Member
 

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>


 
Posted : 09/03/2015 3:29 pm
Posts: 0
Free Member
 

May I suggest you register yourself on a PHP forum such as http://forums.phpfreaks.com/ for help, you'll get a better response.


 
Posted : 09/03/2015 3:33 pm
Posts: 0
Free Member
 

Cheeky!


 
Posted : 09/03/2015 3:40 pm
Posts: 386
Full Member
Topic starter
 

Success!! Thanks allthepies. The print() function did it. Hope I haven't taken up too much of your time. Much appreciated.


 
Posted : 09/03/2015 3:42 pm
Posts: 1530
Full Member
 

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


 
Posted : 09/03/2015 3:46 pm
Posts: 0
Free Member
 

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


 
Posted : 09/03/2015 3:46 pm
Posts: 0
Free Member
 

Is the sql right? Shouldn't it be WHERE some field = 1;?


 
Posted : 09/03/2015 4:05 pm
Posts: 0
Free Member
 

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


 
Posted : 09/03/2015 4:08 pm

6 DAYS LEFT
We are currently at 95% of our target!