Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,928 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,830 people online right now. Registration is fast and FREE... Join Now!




Article / Category Problem

 
Closed TopicStart new topic

Article / Category Problem

Addiction2Code
18 Mar, 2008 - 04:55 PM
Post #1

New D.I.C Head
*

Joined: 19 Dec, 2006
Posts: 39


My Contributions
Ok, well in this code, I attempt to get all the articles that are associated to a category and display them under the category heading. As you can see at, http://madtast.com?page=viewall it shows the heading more then ounce, I would like the heading to only be shown one time, with a listing of all the categories, if anyone has any better code, or ideas then the following example, I would be very greatfull, I just want this to work. ALL SUGGESTIONS ARE WELCOME...

CODE

<div id="scrollbox">
    <?php
    $vAll = mysql_query('SELECT * FROM categories');
    //if (! $vAll) { print "Bad query: " . mysql_error(); }
while($row = mysql_fetch_array($vAll))
  {
    $cId = $row['id'];
    $cName = $row['name'];
    $vAll1 = mysql_query("SELECT article_id FROM article_categories WHERE category_id = ".$cId." LIMIT 1");
        //if (! $vAll1) { print "Bad query: " . mysql_error(); }
    while($row1 = mysql_fetch_array($vAll1))
    {
        $ivaArticle = mysql_query("SELECT * FROM articles WHERE id=".$row1['article_id']." LIMIT 10");
            //if (! $ivaArticle) { print "Bad query: " . mysql_error(); }
            if(mysql_num_rows($ivaArticle) == 0) { $brZero = 0; }
            else { echo '<font><ul class="menu">'.$cName.'</font><hr class="hrMenu">';  $brZero = 1; }
        while($row2 = mysql_fetch_array($ivaArticle))
        {
            $AdHeading = $row2['title'];
            $AdArticleId = $row2['id'];
            if($AdArticleId != $article_id)
            {

                echo('<li class="mItem"><a href="?read='.$AdArticleId.'">'.$AdHeading.'</a></li> ');
            }
        }
            if($brZero == 0) { }
            else { echo '<br><br></ul>'; }
    }


  }
    ?>
</div>

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Article / Category Problem
19 Mar, 2008 - 11:26 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Good start, but the better way is to merge your last two queries together through the table keys. For instance...

php

// This is saying, pull all fields from the articles table where the article category article ids link to the articles id
// (this matches up articles in one table to the articles in the articles table)
// and where the category id is a specific category (provides the limitation on the records returned).

Select articles.* from article_categories, articles where article_categories.articleid = articles.id and article_categories.category_id = $cId";



So hopefully this makes some sense and that it does what it needs to do. This will prevent you from having to do a cascading style of code and should cut your lines in half. The format of the results will be all article records that match the specific category id.

Enjoy!

"At DIC we be sql merging code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

Addiction2Code
RE: Article / Category Problem
19 Mar, 2008 - 01:26 PM
Post #3

New D.I.C Head
*

Joined: 19 Dec, 2006
Posts: 39


My Contributions
Thanks Man! This actually worked, and only took me like, Five minutes to implement, This was GREATLY Appreciated!! You saved me a LOT of work.
User is offlineProfile CardPM
+Quote Post

Closed TopicStart new topic
Time is now: 12/3/08 10:29PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month