Jump to content

[SOLVED] Sorting articles by section/topic with php and mysql


K3nnnn

Recommended Posts

Im currently learning Php and and mysql using various books, this site, and by disecting free third party scripts. On my test site im using a php page to display the five most recently submited articles, which are saved to a Mysql database. Here is the said code which querys the DB

// Execute the query to retrieve articles
[color=red]$result = $connector->query('SELECT ID,title,tagline,thearticle,datesubmited,section,poster FROM cmsarticles ORDER BY ID DESC LIMIT 0,5');[/color]

note ive left the Php tags out!

using the following to echo it

[color=red]// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){

echo '<div class="content_header">';
echo '<img src="../img/greyscale_icons/article.gif" width="34" height="26" /> ';

echo '<a class="article_link" href="viewArticle.php?id='.$row['ID'].'">';
echo $row['title'] ;
echo '</a>';

echo ' ';
echo ' ';
echo 'posted under-';

echo '<span class="article_section">';
echo $row['section'] ;
echo '</span>';

echo '</div>';

echo '<span class="tagline">',$row['tagline'];
echo ' <img src="img/right_quote.png" alt="Quote" />';
echo '</span>';
echo '</p>';
echo $row['thearticle'];
echo '<br/><br/>';
echo 'posted by ';
echo '<span class="article_poster">',$row['poster'];
echo '</span>';
echo ' ';
echo $row['datesubmited'] ;[/color]

But i want to have seperate page for each section/topic the article was posted on, doing exactly the same thing (the latest five, decending order) but say i only wanted articles posted under Tutorials shown in last 5 decending order what query would i make to do this.

many thanks
Link to comment
Share on other sites

guessing this would be the correct way from what you are saying:

[code]// Execute the query to retrieve articles
$result = $connector->query('SELECT section,ID,title,tagline,thearticle,datesubmited,poster FROM cmsarticles ORDER BY ID DESC LIMIT 0,5');[/code]

opposed too

[code]// Execute the query to retrieve articles
$result = $connector->query('SELECT ID,title,tagline,thearticle,datesubmited,section,poster FROM cmsarticles ORDER BY ID DESC LIMIT 0,5');[/code]

am i correct in saying this, or is the code placement wrong, as you can guess im a noob

Link to comment
Share on other sites

This is what ive come up with but im not sure its ok, im not at my test server at the moment so i cannot test it if its not could you give me some more pointers please as im pulling my hair out [code]// Create an object (instance) of the DbConnector
$connector = new DbConnector();

//Execute the query to first retrieve the sections
$section = $connector->query('SELECT section FROM cmsarticles');

// Get an array containing the results of which section.
// Loop for each item in that array
while ($row = $connector->fetchArray($section)){


// Execute the query to retrieve articles
$result = $connector->query('SELECT ID,title,tagline,thearticle,datesubmited,section,poster FROM cmsarticles ORDER

BY ID DESC LIMIT 0,5');


// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){[/code]
Link to comment
Share on other sites

i feel you have now put me on the right track but when i test it on my server which im now at it throws up the following error


Parse error: parse error, unexpected $end in E:\blaa\bla\blah\Root\tutorials.php on line 79 here are my change to the query[code]
//Execute the query to first retrieve the sections
$section = $connector->query('SELECT section FROM cmsarticles GROUP BY section');

// Get an array containing the results of which section.
// Loop for each item in that array
while ($row = $connector->fetchArray($section)){


// Execute the query to retrieve articles
$result = $connector->query('SELECT ID,title,tagline,thearticle,datesubmited,section,poster FROM cmsarticles ORDER

BY ID DESC LIMIT 0,5');


// Get an array containing the results.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){[/code] what could the reason be for this apart from im a noob?
Link to comment
Share on other sites

  • 2 weeks later...
Thanks for the pointers Fenway ive now got the above problem sorted using the following [code]// Create an object (instance) of the DbConnector
$connector = new DbConnector();

//Execute the query to first retrieve the section names
$result = $connector->query('SELECT ID, title, tagline, thearticle, datesubmited, poster, section FROM cmsarticles WHERE section = "Tutorials"');

// Get an array containing the results of which section.
// Loop for each item in that array
while ($row = $connector->fetchArray($result)){[/code]


As you can see i first made a for query for ID,title,tagline etc...  from my article table where the section equals, which i then created an array of
cheers
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.