Updating tables and Array checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Updating tables and Array checking

Post by tomnoble »

I am having problems with this code:

Code: Select all

<?php
$Set_drop = '';
$Set_price = '';
$Set_export = '';

function products_drop()
{
If ($Set_drop != '')
{
mysql_connect("localhost", "dbuser", "dbpass") or die("Connection Failed");
mysql_select_db("dbname")or die("Connection Failed");

$result = mysql_query("SELECT nid FROM drupal_node WHERE type = 'product'"); //just get nid not *


while($row = mysql_fetch_array( $result )) {
   node_delete($row['nid']);
}
$sql = "TRUNCATE TABLE `uc_multigeog`";
mysql_query($sql);
}
}

function update_prices()
{
If ($Set_price != '')
{
  
$file_handle = fopen("http://domain/sites/default/files/imports/product_database.csv", "r");

    while (!feof($file_handle) ) 
      {
        $product_information = fgetcsv($file_handle, 2048);
        $Counter += 1;
      }

mysql_connect("localhost", "dbuser", "dbpass") or die("Connection Failed");
mysql_select_db("dbname")or die("Connection Failed");

$sql = "TRUNCATE TABLE `uc_multigeog`";
mysql_query($sql);

$result = mysql_query("SELECT (nid, title)  FROM drupal_node WHERE type = 'product'"); //just get nid not *

for each ($product_information[2] as $value)
{
while($row = mysql_fetch_array( $result )) {   
if(in_array($row['title'], $product_information[2])) 
	{
	    //Insert data into the Drupal_uc_multigeog table
    mysql_query("INSERT INTO drupal_uc_multigeog
    VALUES ($row['nid'], '946', '0.000', '0.000', $product_information[4], 'N;')");
    mysql_query("INSERT INTO drupal_uc_multigeog
    VALUES ($row['nid'], '930', '0.000', '0.000', $product_information[5], 'N;')");
    mysql_query("INSERT INTO drupal_uc_multigeog
    VALUES ($row['nid'], '960', '0.000', '0.000', $product_information[6], 'N;')");
    mysql_query("INSERT INTO drupal_uc_multigeog
    VALUES ($row['nid'], '915', '0.000', '0.000', $product_information[7], 'N;')");
    mysql_query("INSERT INTO drupal_uc_multigeog
    VALUES ($row['nid'], '900', '0.000', '0.000', $product_information[8], 'N;')");
  }
	}
}
}
}

?>

<h2 class="rtecenter">Welcome to the product database administration page.</h2>
<p>&nbsp;</p>
<p><span style="font-size: medium;"><strong><span style="color: rgb(255, 0, 0);">PLEASE NOTE RUNNING ANY TASKS ON THIS PAGE WILL AFFECT YOUR PRODUCT DATABASE AND THE PRODUCTS AVAILABLE ON YOUR WEBSITE.</span></strong></span></p>
<p>Please follow the tasks below add new products to the site:</p>
<p><input type="button" name="Drop Database" value="Drop Database" onclick="(<?php products_drop(); $set_drop = "yes";?>)" /></p>
<p><input type="button" name="Upload Products" value="Upload Products" onclick="(<a target="_blank" href="http://domain/admin/content/node_import"></a>)" /></p>
<p><input type="button" name="Update Prices" value="Update Prices" onclick="(<?php update_prices(); $set_price = "yes";?>)" /></p>
<p><a target="_blank" href="http://domain/folder/Export_Products.php">Export Products</a></p>
<p>&nbsp;</p>
I know that the function products_drop works fine, the section I am having problems with is the update_prices function. It is reading in a .csv value (which contains all the information for about 1000 products.) The 3rd column contains the title, the 5th, 6th, 7th, 8th, 9th, the prices in different currencies. What I am trying to achieve is....

Look in the drupal_node table, pull out the nid value and the title value where the type = "product"
Check the product title for the one in the array made by the .csv import
When its found it, write to the multigeog table with the associated prices and the nid value from the current row
Do this for all products read in from the drupal_node table

Anybody wish to help me solve this? I have managed to put together the code above with the help from people on here (namely AbraCadaver), but there is something wrong with my coding. This is the first ever time I have had to program php so Im still struggling along.

Thanks for your help in advance.

Kind Regards

Tom
Post Reply