Solved

Help with while() function

Posted on 2014-01-06
10
414 Views
Last Modified: 2014-01-07
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

0
Comment
Question by:greenerpastures
10 Comments
 
LVL 75

Expert Comment

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

Ed note: Added an appropriate MySQL Zone ~ Ray
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39760332
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
 
LVL 17

Expert Comment

by:sweetfa2
ID: 39761258
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:greenerpastures
ID: 39763841
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
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 500 total points
ID: 39763961
$user_ids = arrray("9","12","13");
foreach ($user_ids as $user_id)
{

Open in new window

0
 

Author Comment

by:greenerpastures
ID: 39764051
Still getting:
PHP Fatal error:  Call to undefined function arrray()
0
 
LVL 17

Assisted Solution

by:sweetfa2
sweetfa2 earned 500 total points
ID: 39764077
Typo -- array not arrray
0
 

Author Comment

by:greenerpastures
ID: 39764196
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
 
LVL 17

Expert Comment

by:sweetfa2
ID: 39764208
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
 

Author Closing Comment

by:greenerpastures
ID: 39764262
Actually, it is now working, all I did was replaced "return" with "echo":
echo $display;
}

Thanks so much
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question