0

I have an php array with presentation as follow:-

<?php 
  $ads = array(); 
  $ads [] = array( 
  'name' => 'Apple', 
  'duration' => '3', 
  'price' => "$5"
  ); 

  $ads [] = array( 
  'name' => 'Orange', 
  'duration' => '2', 
  'price' => "$10"
  ); 

  $ads [] = array( 
  'name' => 'Banana', 
  'duration' => '5', 
  'price' => "$6"
  ); 

and then, I would like to replace the static data with dynamic data from database:-

$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);

while($record = mysql_fetch_array($result))
{
$fruit_id = $record['fruit_id'];
$fruit_name = $record['fruit_name '];
$fruit_price= $record['fruit_price'];
$fruit_duration= $record_approve['fruit_duration']; 
}

Actually, how shall I combine the 2 presentations together? Thanks!

Ham
  • 804
  • 3
  • 15
  • 25
  • Is this supposed to be like a "Default data set" in case your table doesn't have "dynamic data" as you called it? – Blake Apr 20 '12 at 04:56
  • 1
    **Please stop writing new code with the ancient `mysql_*` functions.** They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799) . Instead you should learn about `prepared statements` and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). – tereško Apr 20 '12 at 05:07

4 Answers4

1

iteration over result can be modified (provided only the required attributes are fetched in the query)

$fruit = array();    
while($record = mysql_fetch_array($result))
    {
           $fruit[] = $record;
    }

may be you could use array_merge

$result = array();

$result = array_merge($ads,$fruit);
Dhiraj
  • 33,140
  • 10
  • 61
  • 78
1

If you want to get a similar structure as the one that you have in the first example, you can modify your second to create a new array and append it to an existing $ads array.

$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);

$ads = array();
while($record = mysql_fetch_array($result))
{
    $ads[] = array(
              'name' => $record['fruit_name'],
              'price' => $record['fruit_price'],
              'duration' => $record['fruit_duration']);
}
David Z.
  • 5,621
  • 2
  • 20
  • 13
  • Actually, I would like to generate an XML file; is there anything wrong in my codes? $doc = new DOMDocument(); $doc->formatOutput = true; $r = $doc->createElement( "ads" ); $doc->appendChild( $r ); foreach( $ads as $ad ) { $b = $doc->createElement( "ad" ); – Ham Apr 20 '12 at 05:24
  • $name = $doc->createElement( "name" ); $name->appendChild( $doc->createTextNode( $ad['name'] ) ); $b->appendChild( $name ); $duration = $doc->createElement( "duration" ); $duration->appendChild( $doc->createTextNode( $ad['duration'] ) ); $b->appendChild( $duration ); $price = $doc->createElement( "price" ); $price->appendChild( $doc->createTextNode( $ad['price'] ) ); $b->appendChild( $price ); $r->appendChild( $b ); } echo $doc->saveXML(); $doc->save("write.xml") – Ham Apr 20 '12 at 05:24
  • You might want to take a look at this: http://stackoverflow.com/questions/3755952/save-array-as-xml – David Z. Apr 20 '12 at 05:29
  • and this too http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml – Dhiraj Apr 20 '12 at 05:42
0

So, you want to populate your "ads" array with information from the database? It seems fairly counterintuitive seeing as mysql_fetch_array already returns a perfectly adequate associative array, but here you go:

$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);
$ads = array();

while($record = mysql_fetch_array($result))
{
    $ads[] = array (
                       'name' => $record['fruit_name'],
                       'price' => $record['fruit_price'],
                       'duration' => $record['fruit_duration']
                   );
}
Jason Larke
  • 5,289
  • 25
  • 28
0

If

  1. the initial array is not just an example, but default data that you might overwrite with the db data, and

  2. the fruit name is unique (a primary key of sorts)

then you should set the array key of the main array to that of the fruit name, so that if the db has a different value, it will automatically get overwritten but otherwise left alone. Like so:

  $ads ['Apple'] = array( 
  'duration' => '3', 
  'price' => "$5"
  ); 

  $ads ['Orange'] = array( 
  'duration' => '2', 
  'price' => "$10"
  ); 

  $ads ['Banana'] = array( 
  'duration' => '5', 
  'price' => "$6"
  ); 


$sql = "SELECT * from tb_fruit order by fruit_id ASC";
$result = mysql_query($sql_approve, $conn_fruit);

$ads = array();
while($record = mysql_fetch_array($result))
{
    $ads[$record['fruit_name']]['price']    = $record['fruit_price'];
    $ads[$record['fruit_name']]['duration'] = $record['fruit_duration'];
}

Now, if 'Apple' is in the db, the price and duration get overwritten, but otherwise, it stays where you set it before the query.

You could go a step further and have both the price and duration returned by the query checked for an empty value (Null, "", 0), and only set the value if it is not empty, etc. But that is more a of business logic decision.

Anthony
  • 36,459
  • 25
  • 97
  • 163