Array Searching

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

Array Searching

Post by tomnoble »

Hi all,

I have the following code:

Code: Select all

function term_insert($String1, $String2)
  {
    $countries = explode("|", $String1);
    $subs = explode("|", $String2);
    $treference = mysql_query("SELECT * FROM drupal_term_node");
    for each ($countries as $value)
      {
        <Find $value in the $treference array and lookup the assigned tid value>
        //Insert data into the Drupal_term_node table
        mysql_query("INSERT INTO drupal_term_node
        VALUES ($NID_Value, $VID_Value, $TID_Value)");
          for each ($subs as $value1)
            {
              $value1 == $value.$value1 //concatinate the two values
              <Find $value1 in the $treference array and lookup the assigned tid value> 
              //Insert data into the Drupal_term_node table
              mysql_query("INSERT INTO drupal_term_node
              VALUES ($NID_Value, $VID_Value, $TID_Value)");
            }
       }
  }
What is is doing is reading in all the values from a mysql table, placing them in an array and then taking a string apart and finding the values in the table.

The part I am having trouble with is this:

I have a value, I want to find that value in the array I have made.

So I want to search the array $treference for $value, and when it is found, return the row it is on.
If it is not found, then skip the loop and move onto the next value.

Any ideas if I can do this?

Kind Regards

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Searching

Post by AbraCadaver »

First, $treference is not an array, its a mysql result resource. Do something like this:

Code: Select all

$treference = mysql_query("SELECT * FROM drupal_term_node");

while($row = mysql_fetch_assoc($treference)) {
   if(in_array($row['some_field'], $countries)) {  //some_field is whatever field you are looking for the county in
      $TID_Value = $row['tid'];
      //do stuff
   }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: Array Searching

Post by tomnoble »

Hi AbraCadaver, thanks for the reply.

Could I post the first sentence at the top of my module and then use the rest within a function? As I don't want to make unnecessary database calls.

The code is being used to complete mass uploads for a products database, so if I am having 350 consecutive calls to the DB it is going to become very innefficient.

Thanks for your help so far, very useful.

Kind Regards

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Searching

Post by AbraCadaver »

If I understand correctly, you might be better off building the array from the database first and then passing that to function(s):

Code: Select all

$treference = mysql_query("SELECT * FROM drupal_term_node");

while($row = mysql_fetch_assoc($treference)) {
   $db_rows[] = $row;
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: Array Searching

Post by tomnoble »

I wonder if you could tell me what I am doing wrong here:

Code: Select all

  {
   $Counter1 = 1
   $Counter = 1;
   $file_handle = fopen("product_database.csv", "r");

    while (!feof($file_handle) ) 
      {
        $product_information = fgetcsv($file_handle, 2048);
        $Counter += 1;    
      }
      fclose($file_handle);     
      For (Counter1 <= Counter)
        {
          node_insert($product_information[$Counter1, 2]);
          content_type_insert();
          url_insert('Node/'$NID, 'Catalog/'$product_information[$Counter1, 2]);
Apparently, there is something wrong with my For (Counter...) and Something wrong with the way I am referencing the array. node_insert is a function waiting to be passed the value, which will be stored in the array in row = Counter1, column 2.

Any ideas?

Kind Regards

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Searching

Post by AbraCadaver »

There's a lot of problems with that code, but it's not complete. Is there something missing at the end? A counter increment, a closing } ? Paste more and I'll check it out.

1. you fclose() and then try and use the file handle again
2. For (Counter1 <= Counter) //this is not a valid for and those aren't variables (where's the $)
3. Multidimensional arrays aren't referenced like $var[1, 2], they are referenced like $var[1][2]
4. I don't think you have a multidimensional array
5. Your loops are not nested so you will only have the last $product_information that was retrieved
6. There's really no reason to use counters here.
Last edited by AbraCadaver on Thu Jul 01, 2010 1:49 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: Array Searching

Post by tomnoble »

Thanks for the quick reply.

Sorry for the reduced code, here is the function in its entirety:

Code: Select all

function csv_access()
  {
   $Counter1 = 1;
   $Counter = 1;
   $file_handle = fopen("ProductDatabase.csv", "r");

    while (!feof($file_handle) ) 
      {
        $product_information = fgetcsv($file_handle, 2048);
        $Counter += 1;    
      }
      fclose($file_handle);     
      For ($Counter <= $Counter)
        {
          $toinsert = $product_information[2];
		  node_insert($toinsert);
          content_type_insert();
          url_insert('Node/'. $NID, 'Catalog/'.$product_information[2, $Counter1]);
          products_insert('00000'Counter1, create_hash($Counter1, $product_information[$Counter1, 2]));
          term_insert($product_information[$Counter1, 0], $product_information[$Counter1, 1]);
          multigeog_insert($product_information[$Counter1, 4], $product_information[$Counter1, 5], $product_information[$Counter1, 6], $product_information[$Counter1, 7], $product_information[$Counter1, 8]);
          comment_insert();
          access_insert();
          revisions_insert($product_information[$Counter1, 2], $product_information[$Counter1, 3], $product_information[$Counter1, 3]);
          history_insert();
          files_insert($product_information[$Counter1, 9]);
          content_type_insert();
          image_cache_insert();
          echo 'Test';
        }
  }
I have changed the counters to proper variables as you suggested.

Kind Regards

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Searching

Post by AbraCadaver »

It's difficult for me to decipher your code, but this is my best guess:

Code: Select all

error_reporting(E_ALL);
ini_set('display_errors', '1');

function csv_access() {
	$file_handle = fopen("product_database.csv", "r");
	
	$Counter = 1;
	
	while (!feof($file_handle)) {
		$product_information = fgetcsv($file_handle);
		
		node_insert($product_information[2]);
		content_type_insert();
		url_insert('Node/'.$NID, 'Catalog/'.$product_information[2]);
		products_insert('00000'.$Counter, create_hash($Counter, $product_information[2]));
		term_insert($product_information[0], $product_information[1]);
		multigeog_insert($product_information[4], $product_information[5], $product_information[6], $product_information[7], $product_information[8]);
		comment_insert();
		access_insert();
		revisions_insert($product_information[2], $product_information[3], $product_information[3]);
		history_insert();
		files_insert($product_information[9]);
		content_type_insert();
		image_cache_insert();
		
		$Counter++;
	}
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: Array Searching

Post by tomnoble »

I must admit my coding is pretty bad.

I am testing the code you supplied now, and it does this:

Code: Select all

function csvaccess() {
        global $NID_Value, $Product_Title;
        
        $file_handle = fopen("product_database.csv", "r");
        
        $Counter = 1;
        
        while (!feof($file_handle)) {
                $product_information = fgetcsv($file_handle);
                node_insert();
But doesn't ever move on from the node_insert function. Which looks like this:

Code: Select all

function node_insert()
  {
    global $Timestamp,$NID_Value, $NID_Value, $Product_Title;
    //Insert data into the Drupal_node table
    mysql_query("INSERT INTO drupal_node (type,title,uid,status,created,changed,comment,promote,moderate,sticky,tnid,translate)
    VALUES ('product', $Product_Title, '1', '1', $Timestamp, $Timestamp, '0', '0', '0', '0', '0', '0')");
    $NID_Value = mysql_insert_id();
    $VID_Value = $NID_Value;
    mysql_query("UPDATE drupal_node SET vid = $NID_Value WHERE nid = '$NID_Value'");
  }
For some reason, the "title" field in that table which has a type of varchar(255), wont accept a string in the variable $Product_Title. The first value of $Product_Title = "Product 1"

Any ideas on this? I have been trying to work this out for over an hour now.

Kind Regards

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Array Searching

Post by AbraCadaver »

For one, you need to quote text values in the query (look at $Product_Title):
[text]VALUES ('product', '$Product_Title', '1', '1', $Timestamp, $Timestamp, '0', '0', '0', '0', '0', '0')");[/text]
Last edited by AbraCadaver on Fri Jul 02, 2010 9:54 am, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tomnoble
Forum Commoner
Posts: 28
Joined: Tue Jun 15, 2010 1:18 pm

Re: Array Searching

Post by tomnoble »

Thankyou very much. Full complement of working code :D
Post Reply