Jump to content

array_push inside while


nightkarnation

Recommended Posts

Hey guys I have the following script that correctly retrieves 2 values from mysql: security_code and selected_address:

The problem I have, is how can I add the retrieved values (more than 1 row) inside an array called $data

 

$data = array();
$result = mysql_query("SELECT security_code, selected_address FROM purchase WHERE purchase_id = '412012' AND selected_address = 'General Roca 900' ORDER BY `offers_bought`.`security_code` ASC"); 
$cant = 0; 
while($row=mysql_fetch_array($result))
{
array_push($data[$row['selected_address']], $row['security_code']);
        $cant++;
}

 

The array_push function gives me the following error:

Warning: array_push() [function.array-push]: First argument should be an array in C:\wamp\www\Test\test\excelTest.php on line 24

 

In other words this is want I want to achieve:

$data = array(array("selected_address" => $row['security_code']));

Only that this example adds only 1 retrieved row

 

Any ideas?

Thanks in advance!

Cheers,

Link to comment
Share on other sites

Why are you using array_push? I do not think it is relevant for your scenario:

 

while($row=mysql_fetch_array($result))
{
$data[$row['selected_address']] = $row['security_code'];
        $cant++;
}

 

Is what I think you are looking to do.

Link to comment
Share on other sites

First of all thanks a lot for your help guys!

 

Premiso your exactly right! That is what I want to achieve.

The problem that I am having now is coming from the function I have, to export this array to excel, please if you have time take a look:

 

while($row=mysql_fetch_array($result))
{
$data[$row['selected_address']] = $row['security_code'];
}

function cleanData(&$str)
  {
    $str = preg_replace("/\t/", "\\t", $str);
    $str = preg_replace("/\r?\n/", "\\n", $str);
    if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
  }

  # filename for download
  $filename = "website_data_" . date('Ymd') . ".xls";

  header("Content-Disposition: attachment; filename=\"$filename\"");
  header("Content-Type: application/vnd.ms-excel");

  $flag = false;
  foreach($data as $row) {
    if(!$flag) {
      # display field/column names as first row
      echo implode("\t", array_keys($row)) . "\n";
      $flag = true;
    }
    array_walk($row, 'cleanData');
    echo implode("\t", array_values($row)) . "\n";
  }
  exit;

 

Any suggestions?

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.