gli Posted May 16, 2008 Share Posted May 16, 2008 Hi! I've sorted latest news from my database by id. Then the results i fetched array. All is ok. Then i want to show recent subject with news text in main news box, but other last two subjects i want to show in archive news box. And here's my problem that i dont know how to make example third row from that fetched array. Sorry for my bad English. And this is the code: <? // build the query, order by newest ID and limit it to 10 $sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3"; // run the query and set a result identifier to the recordset $res = mysql_query($sql); $article = mysql_fetch_array($res); [color=red]PROBLEM echo $article['subject'] . $article['newstext'] ; // there is information from first row // there is some html elements echo $article['subject']; // there is information from second row // some other html elements echo $article['subject']; // and there is information from third row PROBLEM[/color]?> Thanks Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/ Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Try this? I am not sure if this is what you want... <?php // build the query, order by newest ID and limit it to 10 $sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3"; // run the query and set a result identifier to the recordset $res = mysql_query($sql); while($article = mysql_fetch_array($res)) { echo $article['subject'] . $article['newstext'] ; // there is information from first row // there is some html elements echo $article['subject']; // there is information from second row // some other html elements echo $article['subject']; // and there is information from third row } ?> Question, in the comments you say limit the query to 10 items, but you have a 3? Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542958 Share on other sites More sharing options...
jonsjava Posted May 16, 2008 Share Posted May 16, 2008 you need a while loop: <?php // build the query, order by newest ID and limit it to 10 $sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3"; // run the query and set a result identifier to the recordset $res = mysql_query($sql); while ($article = mysql_fetch_assoc($res)); echo $article['subject'] . $article['newstext'] ; // there is information from first row // there is some html elements echo $article['subject']; // there is information from second row // some other html elements echo $article['subject']; // and there is information from third row ?> Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542959 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Don't you need curly braces for while loops, like in my example? Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542961 Share on other sites More sharing options...
jonsjava Posted May 16, 2008 Share Posted May 16, 2008 *face-palm* yup. his example. not mine*working at same time, sometimes get distracted* Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542965 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 I just wanted to make sure...I doubt myself a lot... Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542969 Share on other sites More sharing options...
gli Posted May 16, 2008 Author Share Posted May 16, 2008 thanks for answers, but problem is not solved. i already tried this. this makes that all that three elements repeats each time and there is more than three results. i need one result with subject and news text second only with subject and third too only with subject and not in list but in unique places. Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542975 Share on other sites More sharing options...
gli Posted May 16, 2008 Author Share Posted May 16, 2008 if you dont understand what i mean, then please tell , and i will make more detailed with picture. thanks Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-542999 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Through a variable (e.g. i) in there. <?php // build the query, order by newest ID and limit it to 10 $sql = "SELECT * FROM news ORDER BY id DESC LIMIT 3"; // run the query and set a result identifier to the recordset $res = mysql_query($sql); $i = 1; while($article = mysql_fetch_array($res)) { if($i == 1) { echo $article['subject'] . $article['newstext']; } else { echo $article['subject']; } $i++; } ?> Link to comment https://forums.phpfreaks.com/topic/105948-problem-with-mysql_fetch_array-rows/#findComment-543048 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.