开发者

Creating a dynamic multidimensional array from MYSQL filters

开发者 https://www.devze.com 2023-03-13 02:59 出处:网络
I have been searching all over for the correct format to create a multidimensional array from a database filter.I am using a constant contact php wrapper for a project which requires an exact format f

I have been searching all over for the correct format to create a multidimensional array from a database filter. I am using a constant contact php wrapper for a project which requires an exact format for loading an array of email addresses, first names, and last names. See below for the correct format of the array the class will accept:

<php?
$contacts = array(
    array(
        'Email Address' => 'test@test.com',
        'First Name' => 'Mike - TEST API',
        'Last Name' => 'NULL',
    )

);

The code as I have it now is below:

<form action="<?php echo $PHP_SELF;?>" method="get" name="form" target="_self" id="form">
<p>
<select name="networks[]" size="50" multiple>
<option value="West Bronx">West Bronx</option>
<option value="Williamsburg">Williamsburg</option>
<option value="Turtle Bay">Turtle Bay</option>
<option value="Times Square">Times Square</option>
<option value="Southeast Bronx">Southeast Bronx</option>
<option value="Sutton">Sutton</option>
<option value="Sunnyside">Sunnyside</option>
<option value="Sheridan Square">Sheridan Square</option>
<option value="Sheepshead">Sheepshead</option>
<option value="Roosevelt">Roosevelt</option>
<option value="Riverdale">Riverdale</option>
<option value="Ridgewood">Ridgewood</option>
<option value="Richmond Hill">Richmond Hill</option>
<option value="Battery Park City Network">Battery Park City Network</option>
<option value="Yorkville">Yorkville</option>
</select>
<br>
<input type="submit" name="Submit" value="Submit">
</form> 

<?



// Get the value of the filters and see if there are more then one selection
$foo = $_GET['networks'];
$sQuery = "";
if (sizeof($foo) > 0) {
  for ($i=0;$i<sizeof($foo);$i++) {
    $sQuery .= "Network LIKE '%$foo[$i]%' OR ";
  }
}
else{
  $sQuery = "Network LIKE '%$foo[0]%";
}
mysql_select_db($database_conn, $conn); 


$query_Recordset0 = "SELECT * FROM contacts WHERE $sQuery "; 
// Knock the last "OR" off the Query
$query_Recordset1 = substr($query_Recordset0, 0, -4); 


echo $query_Recordset1;
$Recordset1 = mysql_query($query_Recordset1, $conn) or die(mysql_error());







?>



<table width="200" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Network</td>
<td>Email</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Recordset1['Network']; ?></td>
<td><?php echo $row_Recordset1['Email']; ?></td>

<td><?php echo $row_Recordset1['First Name']; ?></td>
<td><?php echo $row_Recordset1['Last Name']; ?></td>
</tr>
<?php

//BUILD The Multidimensional Array from the database Query and store it appropriately

$newarray = array();开发者_如何学编程
$newarray[]['Email Address']=$row_Recordset1['Email'];
$newarray[]['First Name']='NULL';
$newarray[]['Last Name']='Last Name';
print_r($newarray);

This produces the array like this:

Array (
[0] => Array ( [Email Address] => )
[1] => Array ( [First Name] => NULL )
[2] => Array ( [Last Name] => Last Name ) )

Array (
[0] => Array ( [Email Address] => silverslammer@aol.com )
[1] => Array ( [First Name] => NULL )
[2] => Array ( [Last Name] => Last Name ) )

Array ( 
[0] => Array ( [Email Address] => shlomi@lmopticaldisc.com )
[1] => Array ( [First Name] => NULL )
[2] => Array

Any help would be greatly appreciated.

Below is what I need it to look like:

$contacts = array(
    array(
        'Email Address' => 'test@test.com',
        'First Name' => 'Mike - TEST API',
        'Last Name' => 'NULL',
    )

);


$newarray[] = array(
  'Email Address'=>$row_Recordset1['Email'];
  'First Name'=>'NULL';
  'Last Name'=>'Last Name';
);

print_r($newarray);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号