PHP Question

Jun 4, 2005
19,723
1
0
<?
while ($row = mysql_fetch_object ($sql)) {
echo "<h3 id='news' title='News'>";
echo $row->title;
print "</h3>";
print "<p>";
echo $row->content;
print "</p>";
}

mysql_free_result ($sql);
?>

When I do that, there's an extra line break before the <h3>news</h3>. Why is this? Is there a better way to pull this SQL information?
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
problem most likely lies with the margins between the p of the last entry and the h3 of the next... i don't think this is a php problem, check your style sheets and try adjusting margins accordingly.
 

Cheetah8799

Diamond Member
Apr 12, 2001
4,508
0
76
Any specific reason you have <p></p> around the content and not the <h3></h> part?

Depending on the browser you use, doesn't <p></p> cause some extra line breaks to break it up into paragraphs? If you just want your data to list out one row at a time, take out those <p> and </p> tags and just put in a <BR> in place of both tags.
 
Jun 4, 2005
19,723
1
0
Using the same tags, but in HTML gives me the desired effect. I'm pulling the title into <h3></h3> and the content into <p></p>. You can see here. The "The Idea" header is on the first line, but here, with the php, "News" is on the second line.

Originally posted by: Cheetah8799
Any specific reason you have <p></p> around the content and not the <h3></h> part?

I missed that. Thanks.

Fixed it, but that wasn't the problem.
 

fs5

Lifer
Jun 10, 2000
11,774
1
0
you're retreiving an empty row on the frist time around. examine the html source of the php one and you'll see:
<div>
<p></p><h3 id="news" title="News"></h3><p></p><h3 id="news" title="News">News</h3>Welcom...
You can either fix this with and empty() check on the row before printing anything out or seeing if you have an empy row in your table. I would do both actually.
 
Jun 4, 2005
19,723
1
0
Originally posted by: fs5
you're retreiving an empty row on the frist time around. examine the html source of the php one and you'll see:
<div>
<p></p><h3 id="news" title="News"></h3><p></p><h3 id="news" title="News">News</h3>Welcom...
You can either fix this with and empty() check on the row before printing anything out or seeing if you have an empy row in your table. I would do both actually.

Thanks! I'll look into that!

I'm so stupid, you were right. Simply an empty row, can't believe I didn't even check that.

Thanks a lot!
 
Jun 4, 2005
19,723
1
0
I was using print for my HTML, echo for my pure PHP. Looks like I used echo for my first line of HMTL, anyways. O_O

Woopsies.
 

wkinney

Senior member
Dec 10, 2004
268
0
0
if your goint to print or echo static html or text, why not just do
<p>
<? echo $row->content; ?>
</p>
etc.
 

mugs

Lifer
Apr 29, 2003
48,920
46
91
Originally posted by: wkinney
if your goint to print or echo static html or text, why not just do
<p>
<? echo $row->content; ?>
</p>
etc.

Or taking it a step further...

<p>
<?= $row->content; ?>
</p>
 
Jun 4, 2005
19,723
1
0
<?while ($row = mysql_fetch_object ($sql)) { ?>
<h3 id="news" title="News">
<?=$row->title; ?>
</h3>
<p>
<?=$row->content; ?>
</p>
<?}mysql_free_result ($sql);?>

Better?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
i don't know about you, but i think the last code is quite ugly and hard to read.
but one thing i should note is that not all servers support shorthand printing (or whatever the term is)

that means:
<?=$row->title;?>

may print the contents of $row->title on one server, but may not do that on another if the ability to parse <?= is turned off.

similarly, not all servers understand that <? .... ?> are php delimiters. It's always safer to use <?php ... ?>

In any case, what you have on the first post is fine (with the exception of maybe changing <? to <?php to ensure compatibility).
Mixing plain text (or plain html) is not a good thing to do especially if it involves nesting of commands. It may look simple and nice for now, but when you're back to reviewing your code months later, you'll simply confuse yourself while trying to decode what it was you were doing.

but then again, it all goes back to personal preferences.
 
Jun 4, 2005
19,723
1
0
Thanks for the input. At this time, the two servers I use support the tags I use. I expect it to remain that way, but if it does change in the future, I can always change it.
 
Jun 4, 2005
19,723
1
0
Problems just keep rolling in.

Added an admin panel, had it working fine but some things were messed up. Fixed the code, but now, for some damn reason, my stylesheet is being echo'd before my admin page, after I log in.

Here's a link to the source of that page : Text

Anyone see a problem?
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
Hmmm... couldn't see the problem from the code you posted.
From my wild guess, it might have something to do with the contents of 'includes/head.php' as it is the first thing included on top of the page.

do you have a sample or screenshot of how the problem looks like for comparison, since we have no idea how it look different?


anyways, hope you don't mind with a few more comments on the code you wrote:

1. $HTTP_SERVER_VARS is deprecated and should not be used anymore. instead, use $_SERVER
2. similarly, for session variables, use $_SESSION

3. you don't need to register or unregister any session variables. simply assign a value to the session variable (eg: $_SESSION['username'] = 'stndn') and it will be registered. also, unsetting the variable (eg: unset ($_SESSION['username'])) will unregister the session variable for you

4. Try to put the { ... } braces even on one-liner if() statements for easier reading and debugging purpose
 
Jun 4, 2005
19,723
1
0
Thanks for the help, but I've written my own code this time and it's working pretty damn good. The site's performing exactly as I'd like it to.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |