...

View Full Version : php issue displaying



JustBlaze
11-12-2007, 08:53 PM
I am using this code:


echo
'<a href="'.
"{$row['id']}".
'">'.
"{$row['name']}</a><br />" .
"{$row['content']}<br />" .
"Posted: {$row['author']}".
'<hr color="white" size="1" />';

to show all the posts from my database, what I was triyng to do was have a horizontal rule between each post but not below the last post or above the first post. Anyone know how to do this?

-Thanks
JustBlaze

mlseim
11-12-2007, 09:16 PM
Not sure if this will help, but it might trigger an idea ...

Always put the line on top, just don't do it for the 1st one.
(that's the part I'm not sure about ... ignoring the 1st one).

if(... it's the first one ...){
echo"
<div style=\"width:200px;\">
<a href=\"{$row['id']}\">{$row['name']}</a><br />
{$row['content']}<br />
Posted: {$row['author']}
</div>
";
}
else{
echo"
<div style=\"border-top:1px solid #000; width:200px;\">
<a href=\"{$row['id']}\">{$row['name']}</a><br />
{$row['content']}<br />
Posted: {$row['author']}
</div>
";
}

JustBlaze
11-12-2007, 09:21 PM
yea that gave me an idea, but I need to figure out how to detect if it's the first or last post on the page.

CFMaBiSmAd
11-12-2007, 09:42 PM
If this is in a loop, before the loop, set a variable - $first_pass = true; Then in the loop you can check if($first_pass){do something for the first pass} or if(!$first_pass){do something if not the first pass} and simply set the variable to false near the end of the loop after you have done any conditional testing of its value - $first_pass = false;

aedrin
11-12-2007, 10:05 PM
The other solution is using an array, although with large texts this might not be as useful as the others. But it might help with other situations, as in my opinion this is cleaner code.



$list = array();
$list[] = "<p>Item 1</p>";
$list[] = "<p>Item 2</p>";
$list[] = "<p>Item 3</p>";

echo implode("<hr />", $list);

JustBlaze
11-12-2007, 10:27 PM
Well I thought what CFMaBiSmAd said would work but now that I thought about it I don't think it will.


$query = "SELECT id, name, content, author FROM blog";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo
'<a href="'.
"{$row['id']}".
'">'.
"{$row['name']}</a><br />" .
"{$row['content']}<br />" .
"Posted: {$row['author']}";

That's the important part of my code that shows the blog entries.
Still can't figure out how to do what I'm trying to do, as the supplied
examples don't seem to fit the situation.

aedrin
11-12-2007, 11:38 PM
My feelings are hurt. I gave an equally good solution and I get no thanks.

I call firstism! (Discrimination based on being the first or not)

JustBlaze
11-12-2007, 11:39 PM
My feelings are hurt. I gave an equally good solution and I get no thanks.

I call firstism! (Discrimination based on being the first or not)

I'm lost on how to use your suggestion though, I appreciate you posting it but can you help me implement it?, I'm still new to php so certain things I'm lost on.

edit: does the same thing as mine and has the same issues :(

psykx
11-13-2007, 01:02 AM
$i = 0
if ( $i = 0 )
{
display row with no bar
}else{
display row with bar
}


You don't need to find the last row if the bar is above the tine of text, search for off by one error to understand why.

JustBlaze
11-13-2007, 01:24 AM
not sure how that's suppose to work :/

this is how it looks using this code:


<?php
$v = $_GET['v'];
if ($v == "cat")
{ echo "Coming Soon";}
else

include 'config.php';
include 'opendb.php';

$query = "SELECT id, name, content, author FROM blog";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo
'<a href="'.
"{$row['id']}".
'">'.
"{$row['name']}</a><br />" .
"{$row['content']}<br />" .
"Posted: {$row['author']}<hr />";
}
include 'closedb.php';
?>
http://img218.imageshack.us/img218/3866/bloggq5.jpg

I'm trying to have it so that there's no horizontal rule under the last post/row that is displayed.

-Hope this lets you better understand my issue.

psykx
11-13-2007, 01:30 AM
ok... I have misunderstood you again what exactly do you mean by horizontal rule?

I though you meant an image or such to divide the results up e.g

result1
--------
result2
--------
result3

CFMaBiSmAd
11-13-2007, 01:41 AM
http://w3schools.com/tags/tag_hr.asp

psykx
11-13-2007, 02:07 AM
<?php
$v = $_GET['v'];
if ($v == "cat")
{ echo "Coming Soon";}
else

include 'config.php';
include 'opendb.php';

$query = "SELECT id, name, content, author FROM blog";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$i = 0
if ( $i = 0 )
{
echo
'<a href="'.
"{$row['id']}".
'">'.
"{$row['name']}</a><br />" .
"{$row['content']}<br />" .
"Posted: {$row['author']}";
$i++;

}else{
echo "<hr />" .
'<a href="'.
"{$row['id']}".
'">'.
"{$row['name']}</a><br />" .
"{$row['content']}<br />" .
"Posted: {$row['author']}";
}
}
include 'closedb.php';
?>

I was right with what I thought you meant I just didn't explain my code properly :) or add the $i++;

JustBlaze
11-13-2007, 02:16 AM
Tried that but it shows a horizontal rule above the first post, the problem that I'm having with all these methods is that it runs the while statement multiple times until there are no more rows left to show, so it puts the horizontal rule on all of the posts.

-Thanks for your effort though
JustBlaze

psykx
11-13-2007, 02:27 AM
the above code didn't error?

it should be $i = 0; not $i = 0
and if ( $i == 0 )

you really should do simple syntax checking.

JustBlaze
11-13-2007, 02:38 AM
the above code didn't error?

it should be $i = 0; not $i = 0
and if ( $i == 0 )

you really should do simple syntax checking.

Yea, I fixed that but didn't figure it was worth posting for.

psykx
11-13-2007, 02:44 AM
cool, and It just doesn't work? did you make sure that there is no "<hr />" if the if{then} part of the if statement?

JustBlaze
11-13-2007, 03:31 AM
It works but there's a horizontal rule above the first post.

psykx
11-13-2007, 11:46 AM
paste your exact code.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum