Help with while() function

The code below works to show listings from one user: $user_id = "9" but I need it to show listings from several users, so I tried placing user IDs in an array like:
$user_id = array("9","12","13");
with the while() function, but cannot get the additional records to appear.


function topagents_listing()
{
// builds a list of X number of latest listings

global $config, $current_ID, $listingID, $conn;
require_once($config['basepath'].'/include/misc.inc.php'); 
	require_once($config['basepath'] . '/include/listing.inc.php');
$misc = new misc();

		
///////////////
$user_id = "9";
//Show User Info
$sql = "SELECT userdb_user_first_name, userdb_user_last_name, userdb_creation_date
		FROM " . $config['table_prefix_no_lang'] . "en_userdb
 	       WHERE userdb_id = ".$user_id." limit 1";

$recordSet = $conn->Execute($sql);
		if ($recordSet === false)
		{
			misc::log_error($sql);
		}
$first_name = $misc->make_db_unsafe ($recordSet->fields['userdb_user_first_name']);
$last_name = $misc->make_db_unsafe ($recordSet->fields['userdb_user_last_name']);
$creation_date = $misc->make_db_unsafe ($recordSet->fields['userdb_creation_date']);
//\Show User Info	

		$sql = "SELECT listingsdb_id, listingsdb_title
		FROM " . $config['table_prefix_no_lang'] . "en_listingsdb
 	       WHERE listingsdb_active = 'yes' AND userdb_id = ".$user_id." ORDER BY listingsdb_id DESC limit 15";
$recordSet = $conn->Execute($sql);
		if ($recordSet === false)
		{
			misc::log_error($sql);
		}
		$num_records = $recordSet->RecordCount();


		if ($num_records > 0) {
$display = '<div class="module-hilite4"><div><div><div><div class="moduletable-scroll250">';
$display .= '<h3>' .$first_name. ' ' .$last_name. ' - ' .$num_records. ' Listings -  member since ' .$creation_date. '</h3><ul>';	

			while(!$recordSet->EOF) {
			
			$listing_title = $misc->make_db_unsafe ($recordSet->fields['listingsdb_title']);
			$ID = $misc->make_db_unsafe ($recordSet->fields['listingsdb_id']);
//$date = $misc->make_db_unsafe ($recordSet->fields['listingsdb_creation_date']);

// Setup Image Tags
				$sql2 = "SELECT listingsimages_thumb_file_name,listingsimages_file_name FROM " . $config['table_prefix'] . "listingsimages WHERE listingsdb_id = ".$ID." ORDER BY listingsimages_rank";
				$recordSet2 = $conn->SelectLimit($sql2, 1, 0);
				if ($recordSet2 === false) {
					$misc->log_error($sql2);
				}
				if ($recordSet2->RecordCount() > 0) {
					$thumb_file_name = $misc->make_db_unsafe ($recordSet2->fields['listingsimages_thumb_file_name']);
					$file_name = $misc->make_db_unsafe($recordSet2->fields['listingsimages_file_name']);
					if ($thumb_file_name != "" && file_exists("$config[listings_upload_path]/$thumb_file_name")) {
	// gotta grab the thumbnail image size
						$imagedata = GetImageSize("$config[listings_upload_path]/$thumb_file_name");
						$imagewidth = $imagedata[0];
						$imageheight = $imagedata[1];
						//$shrinkage = $config['thumbnail_width'] / $imagewidth;
                        $shrinkage = 35 / $imagewidth;
						$featured_thumb_width = $imagewidth * $shrinkage;
						$featured_thumb_height = $imageheight * $shrinkage;
						$featured_thumb_src = $config['listings_view_images_path'] . '/' . $thumb_file_name;
	// gotta grab the thumbnail image size
						$imagedata = GetImageSize("$config[listings_upload_path]/$file_name");
						$imagewidth = $imagedata[0];
						$imageheight = $imagedata[1];
						$featured_width = $imagewidth;
						$featured_height = $imageheight;
						$featured_src = $config['listings_view_images_path'] . '/' . $file_name;
					}
				} else {
						if ($config['show_no_photo'] == 1) {
							$imagedata = GetImageSize("/trade/images/nophoto.gif");
							$imagewidth = $imagedata[0];
							$imageheight = $imagedata[1];
							$shrinkage = 35 / $imagewidth;
							$featured_thumb_width = $imagewidth * $shrinkage;
							$featured_thumb_height = $imageheight * $shrinkage;
							$featured_thumb_src = "/trade/images/nophoto.gif";
							$featured_width = $featured_thumb_width;
							$featured_height = $featured_thumb_height;
							$featured_src = "/trade/images/nophoto.gif";
						} else {
							$featured_thumb_width = '';
							$featured_thumb_height = '';
							$featured_thumb_src = '';
							$featured_width = '';
							$featured_height = '';
							$featured_src = '';
						}
}
			
			
			if ($config['url_style'] == '1') {
$latestlisting_url = 'listing-' . $ID;
			//	$latestlisting_url = 'index.php?action=listingview&amp;listingID=' . $ID;
			}else {
				$url_title = str_replace("-", "", $listing_title);
				$url_title = str_replace("/", "", $url_title);
				$latestlisting_url = 'listing-' . urlencode($url_title) . '-' . $ID . '.html';
			}
	
           $display .= '<li> <a href="' . $latestlisting_url . '"><img src="' .$featured_thumb_src . '" width="' .$featured_thumb_width. '" height="' .$featured_thumb_height. '" /> ' .$listing_title . '</a></li>';
		   
$recordSet->MoveNext();
} // end while
if ($num_records > 0) {
$display .= '</ul>';
//$display .= '<b><a href="/user-'.$user_id.'" title="View all listings">View all '.$num_records.' listings by this seller...</a></b>';
$display .= '</div></div></div></div></div>';
}
}
////////////
return $display;


} // end function latestListings

Open in new window

greenerpasturesAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
sweetfa2Connect With a Mentor Commented:
$user_ids = arrray("9","12","13");
foreach ($user_ids as $user_id)
{

Open in new window

0
 
käµfm³d 👽Commented:
Side comment (for correctness):  while is not a function, rather it is a control structure.

Ed note: Added an appropriate MySQL Zone ~ Ray
0
 
Ray PaseurCommented:
I am guessing that the query involved is here:

$sql = "SELECT userdb_user_first_name, userdb_user_last_name, userdb_creation_date
            FROM " . $config['table_prefix_no_lang'] . "en_userdb
              WHERE userdb_id = ".$user_id." limit 1";

If you have several user_id fields that you want to match you would want to modify the WHERE clause in the query.  This man page tells how that's done.
http://dev.mysql.com/doc/refman/5.7/en/select.html
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
sweetfa2Commented:
Change line 12 to
foreach ($array("9","12',"13") as $user_id)
{

Open in new window


and at line 118
}

Open in new window

0
 
greenerpasturesAuthor Commented:
Hi,
I get the following PHP error :

PHP Fatal error:  Function name must be a string


that refers to this line you suggested:
foreach ($array("9","12","13") as $user_id)
{
0
 
greenerpasturesAuthor Commented:
Still getting:
PHP Fatal error:  Call to undefined function arrray()
0
 
sweetfa2Connect With a Mentor Commented:
Typo -- array not arrray
0
 
greenerpasturesAuthor Commented:
I did not see the typo either, but now it is showing only the listings from the lat user only.
So, from the example above $user_ids = array("9","12","13");   it only selects user 13.
0
 
sweetfa2Commented:
You need to look at the structure of what you are presenting.

You are trying to display multiple users with a single user display.

That means that the format for your output either needs to support multiple users or you need to return multiple pages.

I am not sure what you are trying to achieve in terms of results.

For example, line 16 should be returning multiple rows instead of the single row (ie. use in instead of =).

And then further on when you are extracting your listings from the listings table you are again using a single user.

If you are looking for multi-row, multi-column values such as getting all the listings for each of the users at the same time then you should look at doing a single query using table joins to extract all of the data at the one time.

Then displaying the information may be easier for you to manage and would not require the foreach look previously mentioned.

A short solution might be to move the final } past the return $display line and concatenate the display values each time through the loop, and then return the concatenated value to display.
0
 
greenerpasturesAuthor Commented:
Actually, it is now working, all I did was replaced "return" with "echo":
echo $display;
}

Thanks so much
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.