SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, Heres my code:

    PHP Code:
    <html><head><title>Test</title></head><body>
    <? 
    include("connect.inc"); 
    mysql_select_db('news');
    $getnews "SELECT ID, Title, Textt FROM news";
    $varnews mysql_query($getnews) or die("Cannot fetch News");
    while (
    $news mysql_fetch_array($varnews)) {
    $newsdata[] = array("titledata" => $news["Title"], "textdata" => $news["Textt"]);
    }

    foreach(
    $newsdata as $key => $val) {
    $tmptitle[] = $newsdata["titledata"];
    }


    $tmptitle array_unique($tmptitle);
    foreach(
    $tmptitle as $key => $val) {
    print 
    $tmptitle[$key]["titledata"];
    }

    ?>
    </body></html>
    Basically, it should output some titles, if i remove some of the code it does, but it doesn't display unique entires.

    SO, Can anyone find anything wrong with this code?
    At the moment, It outputs nothing at all.

    Thanx for the help

    Peter

  2. #2
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It doesn't look like you quite understand how foreach() loops work. I've corrected your code, but I suggest you read up on this command to understand it a little better.

    PHP Code:
    // same as above...

    foreach($newsdata as $newsitem) {
      
    $titles[] = $newsitem["titledata"];
    }

    $tmptitle array_unique($titles);
    foreach(
    $titles as $title) {
      print 
    $title;

    Out of curiosity, why are you even fetching the text of the news items if you just plan to print out their titles? If you only needed the unique titles, you could do all the work in a MySQL query ("SELECT DISTINCT Title FROM news"), making your script much more efficient.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need the text because lower down, I print the whole news.

    I can't select distinct because, i'm selecting many other things at the same time.

    Thanx for the Code

    Peter

  4. #4
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anyway to do this to multiple fields? I need to do it to 3 Feilds.


    Or Can I do it for one field, then still output the text / ID to go with that field?

    Exmaple:

    Check Title is Unique
    Check Text is Unique
    Check ID is Unique
    Display Title, Text, ID

    OR

    Check Title is Unique
    Disaply Title, Text, ID

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    This really smacks of something that your database should be doing for you... Are you sure you can't just use a DISTINCT query in your database?

    Certainly, you shouldn't be getting duplicate ID's in your result set unless there is something very wrong with your database...
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well someone told me to combines queries, as the page I'm working on would has 5 queries if I didn't combine them.

    Here is the page where you can view what is happening:

    http://www.codingclick.com/weird.jpg

    Here is the code I'm using for that particular section:

    PHP Code:
    <?
    #########################################
    #MAJOR QUERY BELOW
    ########################################
    $sql "SELECT DISTINCT C.ID as CatID, C.Image as Image, C.Name as CatNames, " .
    "S.ID as SubID, S.CID as SubCatID, S.Name as SubName, " .
    "articles.DateAdded, authors.Name, articles.Description, articles.AID, " .
    "articles.Title, articles.Image as AImage FROM categories as C, subcategories as S, " .
    "articles, authors WHERE C.ID = S.CID AND articles.AuthorID=authors.ID ".
    "ORDER BY DateAdded DESC ";

    $query mysql_query($sql) or die("Cannot get Category.");

    // Loop through the results
    while ($getvars mysql_fetch_array($query)) {

    $stuff[] = array(
    "CatID" => $getvars["CatID"],
    "Image" => $getvars["Image"],
    "CatNames" => $getvars["CatNames"],
    "SubID" => $getvars["SubID"],
    "SubName" => $getvars["SubName"],
    "DateAdded" => $getvars["DateAdded"],
    "Name" => $getvars["Name"],
    "Description" => $getvars["Description"],
    "Title" => $getvars["Title"],
    "AID" => $getvars["AID"],
    "AImage" => $getvars["AImage"]);

    }

    // Check if we're starting a new category

    foreach($stuff as $key => $val) {

    $catid $stuff[$key]["CatID"];
    $catname $stuff[$key]["Image"];
    $subid $stuff[$key]["SubID"];
    $subname $stuff[$key]["SubName"];
    $catnames $stuff[$key]["CatNames"];


      if (
    $catid != $oldCatID) {
        
    // Display new category name
            
    echo("<br>$catname<br>");

      }
      
      
    // Record the current category for the next time
     
    $oldCatID $catid

      
    // Display the current subcat

      
    echo("<font color=\"#FFFFFF\">&nbsp; - </font><a href=\"$catnames.php#$subid\"> ".
    "<font color=\"#FFFFFF\">$subname</font></a><br>");

     
    ?>
    If anyone can help me on this it would really help!

    Thanx

    Peter

    P.S, Distinct Doesn't work because of so many queries...combined into 1.
    Last edited by petesmc; Feb 20, 2001 at 14:27.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •