Hey.. I have a table full of links, one of the fields is called page, this is page the link is printed on.
example link --- Page
index.php --- index
about.php --- index
edit.php ---- account
addnew.php --- forum
contact.php --- index
so i want a script that would print only
index,account,forum
as index has been duplicated.
The reason for this is so then, on each separate page, i want to use a while that will print all the links listed with that 'page'
I've been told to use an array, but i haven't managed to figure it out.
// Loop the query result while ( $row = mysql_fetch_assoc( $res ) ) {
// does the current `$pages` index exist? // example: if `$row['page']` is `index`, then $pages['index'] = array(); if ( ! isset( $pages[ $row['page'] ] ) ) { $pages[ $row['page'] ] = array(); }
// Add the `link` to it's corresponding `page` $pages[ $row['page'] ][] = $row['link'];
}
// See what we have print_r( $pages );
The print_r( $pages ); should give you something like this:
// Loop the query result while ( $row = mysql_fetch_assoc( $res ) ) {
// does the current `region` exist? // example: if `$row['region']` is `abcde`, then $regions['abcde'] = array(); if ( ! isset( $regions[ $row['region'] ] ) ) { $regions[ $row['region'] ] = array(); }
// do the same as above, except for the `name` key if ( ! isset( $regions[ $row['region'] ][ $row['name'] ] ) ) { $regions[ $row['region'] ][ $row['name'] ] = array(); }
// add the date and count data to it's corresponding region/name $regions[ $row['region'] ][ $row['name'] ][] = array( 'date' => $row['date'], 'count' => $row['count'], );
Thank you very much Keith, that code really worked. I've been searching all over and others suggestions took me to nesting tables, pivoting, etc., Very confusing. Your code is clean and it does what the OP asked and what I was looking for. Again, thank you