Jump to content

Archived

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

markspec87

Retreiving news - simplier way?

Recommended Posts

I currently use the code
[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']; ?>[/code]

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 :)

Share this post


Link to post
Share on other sites
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.
[code]
$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
}
[/code]

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

Hope it helps.

Share this post


Link to post
Share on other sites

×

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.