Jump to content


Photo

Retreiving news - simplier way?


  • Please log in to reply
1 reply to this topic

#1 markspec87

markspec87
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts

Posted 15 October 2006 - 11:22 PM

I currently use the code
<? define ('DB_USER', 'hidden');
define ('DB_PASSWORD', 'hidden');
define ('DB_HOST', 'hidden');
define ('DB_NAME', 'hidden');

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die('Failure: ' . mysql_error() );
mysql_select_db(DB_NAME, $dbc) or die ('Could not select database: ' . mysql_error() );

$query="SELECT * FROM news ORDER BY Datum";
$result=mysql_query($query);

$num=mysql_numrows($result);


$i=0;
while ($i < $num) {

$author=mysql_result($result,$i,"author");
$text=mysql_result($result,$i,"text");
$datum=mysql_result($result,$i,"Datum");
$readmore=mysql_result($result,$i,"readmore");
$image=mysql_result($result,$i,"image");
$Title=mysql_result($result,$i,"title");
$summary=mysql_result($result,$i,"summary");
$i++;
}

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die('Failure: ' . mysql_error() );
mysql_select_db(DB_NAME, $dbc) or die ('Could not select database: ' . mysql_error() );

$query="SELECT * FROM news ORDER BY datum DESC LIMIT 1,1";
$result=mysql_query($query);
$rw = mysql_fetch_assoc($result);
$author2=$rw['author'];
$text2=$rw['text'];
$datum2=$rw['Datum'];
$readmore2=$rw['readmore'];
$image2=$rw['image'];
$newstitle2=$rw['title'];
$summary2=$rw['summary']; ?>

To retrieve the most recent and second most recent fields in news. Then i just call the variables for display on my news page (i have another page where news is submitted).

Im wondering if theres a simplier or more efficient way to do this? on some pages i have 5 news articles shown and i simply repeat the above but for 5 rows, surely theres a better way?

any help would be appreciated :)

#2 Stooney

Stooney
  • Members
  • PipPipPip
  • Advanced Member
  • 1,093 posts
  • LocationLas Cruces, NM

Posted 15 October 2006 - 11:49 PM

yes actually, but it would be alot different than your doing.  Instead of setting variables like author1 author2 etc, try outputting text 'as you go':  for ex.
$result=@mysql_query("SELECT * FROM news ORDER BY Datum limit 2"); //limit 2 means you only want the 2 most recent

while($row=mysql_fetch_array($result))                                                //get the data 1 news post at a time
{

now you can go ahead and display the news topic.  $row[0] would be the first table cell in your db table.  so 
say you have your table as   author, text, datum, readmore, image, title, summary then $row[0] is author, $row[3] is 
readmore and so on.   for example:

         echo "Author of article: $row[0]";   //displays the author we got from our query
}

The while statement will repeat as long as the array is, in this case twice.

Hope it helps.

<embed src='.wav' autostart='true' loop='false'
width='2' height='0'>
</embed>
i found that ERMMMMM thats called PHP!!!!





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users