Jump to content

is this correct?


stuart7398

Recommended Posts

hi.

i'm learning mysql / php.

i'm interested as to whether the following code is the correct way to disply data and text inbetween?

 

thanks.

 

<?php

require '../conn.php';

require '../conn.php';

$data = mysql_query("SELECT * FROM boats ORDER BY date DESC LIMIT 10")

or die(mysql_error());

 

Print "<h1>Boats</h1>";

Print "<div class='text'>";

Print "text text text";

Print "<br />";

Print " text text text";

Print "</div><br /><div><div class='heading'>Our latest</div>";

Print "<div class='gallery'>";

while($info = mysql_fetch_array( $data ))

{

Print "<p><a href='boats.php?id=".$info['submission_id']."'>".$info['col_2'] ." <img src='/imgs/bg/boats.jpg' width='73px' /><i>".$info['submission_id'] . "</i></a></p>";

}

Print "</div></div>";

 

require '../conn.php';

?>

 

Link to comment
https://forums.phpfreaks.com/topic/97456-is-this-correct/
Share on other sites

I'd say overall you have the right idea. However, there are a few things that I would suggest to you.

 

1. Wrap your code in


tags when posting to the forum.

2. Use double quotes for HTML attributes. That may mean that you'll need to escape them.

3. Backtick your table and field names in MySQL. Especially since you're using a MySQL reserved word (date) as one of your field names.

4. Don't always assume that you'll get something back from your query. You should do some kind of test that checks to see if you have any rows in your result.

5. If I'm not mistaken, you only need to use require once.

6. You don't have to, but you can use php variables inside a double quoted string.

If you do "Hello $username!" that would be fine. If you're trying to access a row variable, like you're doing inside your while loop, you can simply wrap them in braces.

So you can say "<div>Hello {$data['username']}</div>" and be fine as well. The best explanation that I could come up with for that is that whatever is inside the braces is evaluated and then put into the string.

 

So with that said, here's what I'd change...

<?php
require '../conn.php';
$data = mysql_query("SELECT * FROM `boats` ORDER BY `date` DESC LIMIT 10") OR DIE (mysql_error());

print "<h1>Boats</h1>";
print "<div class=\"text\">";
print "text text text";
print "
"; // don't really know what you're doing here
print "text text text";
print "</div>
<div><div class=\"heading\">Our latest</div>";
print "<div class=\"gallery\">";
if (mysql_num_rows($data) > 0 )
{
while($info = mysql_fetch_array($data))
{
	print "<p><a href=\"boats.php?id={$info['submission_id']}\">{$info['col_2']}<img src=\"/imgs/bg/boats.jpg\" width=\"73px\" />{$info['submission_id']}</a></p>";
}
}
else
{
// do something for no rows returned
}
print "</div></div></div>";
?>

 

And I think you're missing a closing div.

Link to comment
https://forums.phpfreaks.com/topic/97456-is-this-correct/#findComment-498651
Share on other sites

Also, I did the same thing when I started PHP and someone pointed it out to me. Using multiple print/echos like that.

 

<?php
echo
'First Line'.
'Second Line'.
"You can even have new lines\n".
'or you can do it this way as well'."\n".
"and make sure you finish your echo off";
?>

 

I guess it comes down to preference but I see a lot of forum posts using multiple print/echo statements.

Link to comment
https://forums.phpfreaks.com/topic/97456-is-this-correct/#findComment-498765
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.