• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Problem getting xajax to display all records

I trying to get xajax to assign all records to a div element so far it is only displaying one record of the two that exist. Can anyone tell me where I am going wrong?
<?php
function viewprojects($id) {
	
  $objResponse = new xajaxResponse();
	
   if (!mysql_connect('localhost','kwalton','406002956')) {	
  	
  	return -1;
  }
  
    if (!mysql_select_db('kwalton')) {	
  	die('Could not select the database required to view the user record');
  }
  
	
	$qry = "SELECT * FROM projects WHERE client_id='$id'";
 
	if( ($result = mysql_query($qry)) != null)
		{
 
			$record = @mysql_fetch_assoc($result);
			
		}
	
	if(!$record){
	 $objResponse->alert('Not Found!');
	 return $objResponse;
 
	}
      
	
	if (mysql_errno()) {
		$objResponse->alert('Invalid SQL');
		return $objResponse;
	}
	$ret=array();
      
	 foreach($record as $value => $text)
      {
	$ret []='$text';
   
      }
       
	
	    /*** assign the options to the products div ***/
    $objResponse->assign('SomeElementId', 'innerHTML',$ret );
  
    /*** and return the response ***/
    return $objResponse;
 }
 
?>
<?php
require_once ("xajax/xajax_core/xajax.inc.php");
 
require 'ajax_Client.server.php';
 
$xajax = new xajax();
$xajax->registerFunction('slow_function');
$xajax->registerFunction('myFunction');
$xajax->registerFunction('viewprojects');
 
$xajax->processRequest();
?>
<html>
    <head>
        <title>Loading Bar Demo</title>
        <? $xajax->printJavascript('xajax/'); ?>
	<script type="text/javascript">
        function myclient(id){
 
	 document.getElementById('contact_name').value=xajax_viewclient(id);
	}
      </script>
    </head>
    <body>
                <input type="button" onclick="xajax_slow_function();" value="Slow Function" />
		  <input type="button" onclick="xajax_viewprojects(2);" value="Get Client 2" />
		  <input type="text" id="contact_name" onchange="" value="" />
 
<div id="SomeElementId"></div>
<div id="client_name"></div>
<input type="button" value="Insert" onclick="xajax_myFunction();" />
 
</body>
</html>

Open in new window

0
sprnova
Asked:
sprnova
1 Solution
 
Roger BaklundCommented:
You are just fetching one row:

$qry = "SELECT * FROM projects WHERE client_id='$id'";
 
        if( ($result = mysql_query($qry)) != null)
                {
 
                        $record = @mysql_fetch_assoc($result);
                       
                }


To fetch all, you need a loop:

$qry = "SELECT * FROM projects WHERE client_id='$id'";
$result = mysql_query($qry);
while($one_record = mysql_fetch_assoc($result))  
  $record[] = $one_record;

This won't work:

      foreach($record as $value => $text)
      {
        $ret []='$text';
   
      }
       
            /*** assign the options to the products div ***/
    $objResponse->assign('SomeElementId', 'innerHTML',$ret );

You can try this:

foreach($record as $one_record)
  $ret []= implode(' ',$one_record); # fields separated by space

$ret = implode('<br />',$ret);  # one item per line
/*** assign the options to the products div ***/
$objResponse->assign('SomeElementId', 'innerHTML',$ret);
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now