PHP and MYSQL group by help

Discussion in 'PHP' started by nickharper, Mar 21, 2010.

  1. #1
    Hi,

    I have a database which is going to be storing SEO work that is done, so it will log where backlinks were done, to what page they are directed at and what type of page this is, whether it be a category page, content page etc.

    Here is an example

    Database
    --------

    linksource            page4links         type
    --------------------------------------------------------
    Squidoo			Index		Content
    Social Bookmark		Trojans		Category
    Squidoo			Trojans		Category
    Article			Trojans		Category
    Social Bookmark		Other Category	Category		
    Forum Post		About Us	Content	
    Code (markup):
    That is pretty much how I have the database (minus a few other fields that dont relate to this query). I want to be able to produce the info on a PHP like below, how would I do this?

    		Squidoo		Social Book	Article
    Trojans		Yes		Yes		Yes
    Other Category	No		Yes		No
    Code (markup):
     
    Last edited: Mar 21, 2010
    nickharper, Mar 21, 2010 IP
  2. Lordo

    Lordo Well-Known Member

    Messages:
    2,082
    Likes Received:
    58
    Best Answers:
    0
    Trophy Points:
    190
    #2
    I wouldn't have used GROUP BY because it would have used two sentences. Instead, I would go with this scheme:
    1. SELECT * FROM table ORDER BY page4links, linksource
    2. Convey the records to some arrays
    3. Loop through the arrays and print the values accordingly

    Key points:
    1. while looping, check if the page4links is changed or if it is still the same (using a variable).
    2. by using an array for linksource (e.g. linksource[3] = "Squidoo"), the table will be easy to print.
     
    Lordo, Mar 21, 2010 IP