Solved

Help with while() function

Posted on 2014-01-06
10
402 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 74

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 108

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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now