Page is a not externally linkable
- Code, Content, and Presentation
-- PHP Server Side Scripting
---- display all images which belongs to one user


rlange - 8:28 pm on Jun 11, 2012 (gmt 0)


Davidkarate wrote:
I got everything working with this query, except the result is repeating user information for every image.

Yeah, that's one of the things about SQL; it doesn't return complex objects. It just returns rows.

In your situation, I would probably do two separate queries. One to grab the information about the classified itself (title, description, state, city) and a second one just to grab the images associated with that classified.

<?php

$result = mysqli_query( "SELECT `classifieds`.`title`, `classifieds`.`description`, `state`.`statename`, `city`.`city`
FROM `classifieds`
LEFT JOIN `state` ON `classifieds`.`state_id` = `state`.`id`
LEFT JOIN `city` ON `classified`.`city_id` = `city`.`id`
WHERE `classifieds`.`id` = " . $id . "
AND `authorized` = '1'" );

$classified = mysqli_fetch_assoc( $result );

$result = mysqli_query( "SELECT `image_path` FROM `img` WHERE `classified_id` = " . $id );

$classified['images'] = array();
while( false !== ( $row = mysqli_fetch_assoc( $result ) ) {
$classified['images'][] = $row;
}

var_dump( $classified );

?>

Obligatory Warnings:

  1. Make sure $id is properly sanitized or you open yourself up to SQL injection attacks. This can't be stressed enough.
  2. If you're on PHP 5 or above, use either the mysqli_* [us3.php.net] functions, the MySQL PDO [us3.php.net] driver, or Zend Framework's Zend_Db [framework.zend.com] classes.

Also, the above code is untested and isn't guaranteed to work, but hopefully it gets the point across. :o)

--
Ryan


Thread source:: http://www.webmasterworld.com/php/4464050.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com