PHP MySQL array formatting

dcjsdts
dcjsdts used Ask the Experts™
on
I have no idea what I'm doing - I'll be honest.  Actually, I do but I'm giving up and eating crow.  Below is what I have....   I am connecting to the DB ok.  My "Alerts" show the same results.  Yet my var custodians is INCORRECT and my var custodians2 works fine.   This is related to a JS that populates an autosuggest text field.  It all works fine with I use the literal format in custodians2, but my first one doesnt return anything.  Any ideas?
<?php
$sql_nw = "SELECT custodian FROM custodians";
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$num_nw = mysql_num_rows($rec_nw);
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$url_array=array();
while($rs_nw = mysql_fetch_array($rec_nw))
{
$url_array[]=$rs_nw["custodian"] ;
}
?>
 
<script language="javascript">
 
var custodians = new Array( <?php print json_encode($url_array); ?> );
 
//alert(custodians);
 
var custodians2 = new Array( 'United States', 'Afghanistan', 'Zaire', 'Zambia', 'Zimbabwe' )
//alert(custodians2);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Can you please run the script i posted and give the output here? A reason could be that your sql created a multi-dimensional array.
<?php
$sql_nw = "SELECT custodian FROM custodians";
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$num_nw = mysql_num_rows($rec_nw);
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$url_array=array();
while($rs_nw = mysql_fetch_array($rec_nw))
{
$url_array[]=$rs_nw["custodian"] ;
}
$test=json_encode($url_array);
echo var_dump($test);
echo var_dump($url_array);
?>
 

Open in new window

Author

Commented:
DO you have a double one of these doohickies in there?

$rec_nw = mysql_query($sql_nw) or die(mysql_error());

Commented:
You're right but that shouldnt matter. I just copied your code and added 2 var_dumps. Can you please run the script on your server and post the output here? Because i cant help you with just the code because i dont know the database structure etc.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
string(47) "["Richard Strong","James Magee","ISO Office"]" array(3) { [0]=> string(16) "Richard Strong" [1]=> string(11) "James Magee" [2]=> string(10) "ISO Office" }


:::  Your results


Commented:
Ok, I see the problem. if you need the array in that format:
var custodians2 = new Array( 'United States', 'Afghanistan', 'Zaire', 'Zambia', 'Zimbabwe' )
You should generate a string  instead of using json_encode(). You can use that function to create what you need.  

<?php
function formatit($array){
$string="";
echo count($array);
echo "<br />";
     foreach($array as $index=>$data){
	  if($index!=count($array)-1){
              $string=$string."'$data',";
          }
	  else{
	      $string=$string."'$data'";
          }    
     }
return $string;
}
?>
  
//would be called like this:
$url_array=formatit($url_array);
//in the js:
var custodians = new Array( <?php echo $url_array; ?> );

Open in new window

Author

Commented:
Thanks!!!  I will try to apply it.  It looks a bit challenging but let's see what the next 30 minutes brings.  Is there a PM on here?  if not, please find me on rbsterli on AIM if you wish.  Thanks!

Commented:
Sorry i left a debug notice of  myself in the code(i tested it for you already :)) You can save this code as file and include it into your file. The thing below is your code with the function(should be working now):
$sql_nw = "SELECT custodian FROM custodians";
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$num_nw = mysql_num_rows($rec_nw);
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$url_array=array();
while($rs_nw = mysql_fetch_array($rec_nw))
{
$url_array[]=$rs_nw["custodian"] ;
}
$url_array=formatit($url_array);
function formatit($array){
$string="";
echo count($array);
echo "<br />";
     foreach($array as $index=>$data){
	  if($index!=count($array)-1){
              $string=$string."'$data',";
          }
	  else{
	      $string=$string."'$data'";
          }    
     }
return $string;
}
var custodians = new Array( <?php echo $url_array; ?> );

Open in new window

Commented:
Forget what a said about incude something. Just test the code and ask if there are any more problems.

Author

Commented:
No results.  Just a blank white screen.  I am tryin' my best to get to the bottom of it...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
 
 
<script src="js/getobject2.js"></script>
<script src="js/acdropdown.js"></script>
	
<?php
 
$MySQL_Host = 'localhost';
$MySQL_User = 'rrrr';
$MySQL_Pass = 'bbbbbb';
$MySQL_DB = 'CBBBVCB';
 
 
$db = mysql_connect($MySQL_Host, $MySQL_User, $MySQL_Pass)
	or
die("Could not connect");
mysql_select_db("$MySQL_DB",$db);
 
 
$sql_nw = "SELECT custodian FROM custodians";
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$num_nw = mysql_num_rows($rec_nw);
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$url_array=array();
while($rs_nw = mysql_fetch_array($rec_nw))
{
$url_array[]=$rs_nw["custodian"] ;
}
$url_array=formatit($url_array);
function formatit($array){
$string="";
echo count($array);
echo "<br />";
     foreach($array as $index=>$data){
	  if($index!=count($array)-1){
              $string=$string."'$data',";
          }
	  else{
	      $string=$string."'$data'";
          }    
     }
return $string;
}
 
<script language="javascript">
 
var custodians = new Array( <?php echo $url_array; ?> );
 
 
function formatCountries( sText )
{
return sText.substr( 0, sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ), this.sActiveValue.length ).bold().fontcolor( '#ff0000' ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) + this.sActiveValue.length )
}
function alertSelected()
{
document.getElementById( 'selectedCountry' ).innerHTML = this.sActiveValue
}
</script>
	</head>
	<body>
 
 
 
<input class="dropdown" autocomplete="off" id="inputer16" style="width: 250px;" acdropdown="true" autocomplete_list="array:custodians" autocomplete_format="formatCountries" autocomplete_onselect="alertSelected" autocomplete_matchbegin="false">
 
	</body>
</html>

Open in new window

Author

Commented:
Increasing to 300 pts.  I would like an answer or someone who knows to help me.  contact at rbsterli on AIM or let me know here and I will chat via FB and or/a remote desktop session.

Author

Commented:
Increased to 500.  I need a resolution tonight.
Hi,

Try this code


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

       
 
 
<script src="js/getobject2.js"></script>
<script src="js/acdropdown.js"></script>
       
<?php
 
$MySQL_Host = 'localhost';
$MySQL_User = 'rrrr';
$MySQL_Pass = 'bbbbbb';
$MySQL_DB = 'CBBBVCB';
 
 
$db = mysql_connect($MySQL_Host, $MySQL_User, $MySQL_Pass)
        or
die("Could not connect");
mysql_select_db("$MySQL_DB",$db);
 
 
$sql_nw = "SELECT custodian FROM custodians";
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$num_nw = mysql_num_rows($rec_nw);
$rec_nw = mysql_query($sql_nw) or die(mysql_error());
$url_array=array();
while($rs_nw = mysql_fetch_array($rec_nw))
{
$url_array[]=$rs_nw["custodian"] ;
}
$url_array=formatit($url_array);
function formatit($array){
$string="";
echo count($array);
echo "";
     foreach($array as $index=>$data){
          if($index!=count($array)-1){
              $string=$string."'$data',";
          }
          else{
              $string=$string."'$data'";
          }    
     }
return $string;
}
 ?>
<script language="javascript">
 
var custodians = new Array( <?php echo $url_array; ?> );
 
 
function formatCountries( sText )
{
return sText.substr( 0, sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ), this.sActiveValue.length ).bold().fontcolor( '#ff0000' ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) + this.sActiveValue.length )
}
function alertSelected()
{
document.getElementById( 'selectedCountry' ).innerHTML = this.sActiveValue
}
</script>
       
       
 
 
 
<input class="dropdown" autocomplete="off" id="inputer16" style="width: 250px;" acdropdown="true" autocomplete_list="array:custodians" autocomplete_format="formatCountries" autocomplete_onselect="alertSelected" autocomplete_matchbegin="false">
 
       

Commented:
you still had my debug echo in there. Not your fault it shoult work if  you  delete lines 35 and 36.
Commented:
I tested your version and modified the sql part to a fixed array(you can replace that part with your sql) and it gave me a screen with an input box. No autocomplete but the php was right. You should check your js.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
 
 
<script src="js/getobject2.js"></script>
<script src="js/acdropdown.js"></script>
<?php
$url_array=array("1.element","2.element","3.element","4.element");	 
$url_array=formatit($url_array);
function formatit($array){
$string="";
     foreach($array as $index=>$data){
	  if($index!=count($array)-1){
              $string=$string."'$data',";
          }
	  else{
	      $string=$string."'$data'";
          }    
     }
return $string;
}
?>
<script language="javascript">
 
var custodians = new Array( <?php echo $url_array; ?> );
 
 
function formatCountries( sText )
{
return sText.substr( 0, sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ), this.sActiveValue.length ).bold().fontcolor( '#ff0000' ) + sText.substr( sText.toLowerCase().indexOf( this.sActiveValue.toLowerCase() ) + this.sActiveValue.length )
}
function alertSelected()
{
document.getElementById( 'selectedCountry' ).innerHTML = this.sActiveValue
}
</script>
	</head>
	<body>
 
 
 
<input class="dropdown" autocomplete="on" id="inputer16" style="width: 250px;" acdropdown="true" autocomplete_list="array:custodians" autocomplete_format="formatCountries" autocomplete_onselect="alertSelected" autocomplete_matchbegin="false">
 
	</body>
</html>

Open in new window

Author

Commented:
We have a good bingo!   Thanks!!!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial