Jump to content


Photo

help?


  • Please log in to reply
16 replies to this topic

#1 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 03:45 PM

help me out here i need to retrieve data from a database. its the news and i need to display it in desc order
so the most recent comes first and i need to make sure there isnt more than 5 posts on one page

the table name is "news"
these are the fields:
title
poster
whenposted
textmessage

if u could just start me off i usually can think up this stuff but im drawing blanks :(

#2 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 13 July 2006 - 03:51 PM

$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5",$yourConnection);
while($row = mysql_fetch_assoc($result))
{
 //Your display code  
 echo('Title'.$row['title'].' by '.$row['poster'].'<br />');
echo($row['textmessage'].'<br />');
//ETC...
}

Hope it helps.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#3 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 03:54 PM

would u explain this part a bit i dont get what your doing with the $row
echo('Title'.$row['title'].' by '.$row['poster'].'<br />');
echo($row['textmessage'].'<br />');


#4 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:01 PM

ne body i know u can help

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 July 2006 - 04:06 PM

$row is an array. The array was created with this bit of code:
while($row = mysql_fetch_assoc($result))

title, poster and textmessage is the name of your field names within your table that you are querying for results.

#6 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 04:06 PM

$row is fetching the array from the database. The mysql_fetch_array() function is what pulls the information out.

The $row['title'] part is where you are telling it from which column to take it. You're using the method MYSQL_ASSOC (associative, as opposed to numeric) so you call the array based on the name of the column.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#7 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:08 PM

ok now i get lol srry i am still kinda tired i had a Mountain Dew

#8 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:20 PM

i put in this bit of code
<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5")
while($row = mysql_fetch_assoc($result))
{
$newtime = $row['whenposted']
$time = gmdate("H:i:s m/d/Y", $newtime);
											
echo("<center>.$row['title'].<br>.' by '.$row['poster'].' on '.$time.<br>")
echo("$row['textmessage'].<br><hr></center>")
}
?>

but got this error

Parse error: syntax error, unexpected T_WHILE in /home/dreamsh/public_html/test/index.php on line 103

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 04:27 PM

Missing semi-colon at the end of this line:
<?php $result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5") ?>
These two lines:
<?php
echo("<center>.$row['title'].<br>.' by '.$row['poster'].' on '.$time.<br>")
echo("$row['textmessage'].<br><hr></center>")
?>
Need to be written as:
<?php
echo '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';
echo $row['textmessage'] . '<br><hr></center>';
?>

Ken

#10 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:31 PM

yes its in a config.php file and at the top of this page has
<?php 
include("config.php")
?>


now that should of fixed it but this is the error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/dreamsh/public_html/test/index.php on line 107

<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5");
while($row = mysql_fetch_assoc($result)){
$newtime = $row['whenposted'];
$time = gmdate("H:i:s m/d/Y", $newtime);
											
echo(" '<center>'.$row['title'].'<br>'.' by '.$row['poster'].' on '.$time.'<br>' ");
echo("$row['textmessage'].'<br>'.'<hr>'.'</center>'");
}
?>


#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 04:34 PM

Fix those two echo lines like I said in my previous post.

Ken

#12 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 13 July 2006 - 04:36 PM

You are missing the ; again at the end of include("config.php")

Also, your 107/108 line code should be:
echo("<center>$row['title']<br /> by $row['poster'] on $time <br />"); //this is line 107
echo("$row['textmessage'] <br /><hr></center>");

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#13 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 04:37 PM

<?php
include("config.php");
?>

Dont forget the semi-colon, I'm not sure it matters in this case above, but it's a good habbit.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#14 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:40 PM

ok heres the latest

<?php
$result = mysql_query("SELECT * FROM news ORDER BY whenposted DESC LIMIT 5");
while($row = mysql_fetch_assoc($result)){
$newtime = $row['whenposted'];
$time = gmdate("H:i:s m/d/Y", $newtime);
											
echo("<center>$row['title']<br /> by $row['poster'] on $time <br />");//line 107
echo("$row['textmessage'] <br /><hr></center>");
}
?>

still this error

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/dreamsh/public_html/test/index.php on line 107

#15 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 July 2006 - 04:43 PM

Those two echo lines need to be changed to:
<?php
echo '<center>' . $row['title'] . '<br>by ' . $row['poster'] . ' on ' . $time . '<br>';
echo $row['textmessage'] . '<br><hr></center>';
?>

I posted this back in reply #8

Ken

#16 BillyBoB

BillyBoB
  • Members
  • PipPipPip
  • Advanced Member
  • 630 posts

Posted 13 July 2006 - 04:47 PM

thx so much

this was a great learning experince which make me a lvl 50 coder lol

#17 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 13 July 2006 - 04:48 PM

Just remember to put semi-colons at the end of each line when appropriate because it'll always spit out errors.

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users