Jump to content

Help With Arrays


php4ever

Recommended Posts

I have a text file that has about 2,000 rows of data. Unfortunately it also has column headers and my script will not skip the first row regardless of all my attempts.

 

 

   

BUSOEMPS

   

BUSOLSTR

   

BUSOPRKG

 

 

   

7

   

No

   

Yes

 

 

   

3

   

Yes

   

No

 

 

   

6

   

Yes

   

No

 

 

   

0

   

Yes

   

Yes

 

 

   

0

   

No

   

Yes

 

 

   

7

   

Yes

   

Yes

 

 

   

0

   

No

   

No

 

 

   

0

   

NO

   

No

 

 

   

0

   

NO

   

Yes

 

 

   

0

   

Yes

   

Yes

 

 

   

0

   

No

   

Yes

 

 

   

0

   

No

   

Yes

 

 

   

6

   

Yes

   

Yes

 

 

   

0

   

No

   

Yes

 

 

require_once('dbsettings.php');
$sqltablepref = "set3_";

$insert_fields=array(
"BUSOEMPS"=>"0",
"BUSOLSTR"=>"1",
"BUSOPRKG"=>"2"
);

function explode_features($insert_fields_features, $features) {
   $elements = explode(",", $features);
   $newStr = "";
   $i=0;
   foreach ($elements as $field_element)
   {
   //echo($bit);
        if (array_key_exists($field_element,$insert_fields_features))
        {
        	if ($insert_fields_features[$field_element])
        		$newStr .= $insert_fields_features[$field_element].", ";
        }
   }
   return substr($newStr,0,-2);
}
$specified_user_id="";
$sql="select userdb_id from ".$sqltablepref."userdb where userdb_user_name like 'Manager'";
$select=mysql_query($sql);
$rows=mysql_num_rows($select);
if($rows==1)
{
$row=mysql_fetch_array($select);
$specified_user_id=$row[0];
}
else
$specified_user_id=2;
$specified_id="";
$select=mysql_query($sql);
$rows=mysql_num_rows($select);
if($rows==1)
{
$row=mysql_fetch_array($select);
$specified_id=$row[0];
}
else
$specified_id=2;
$fp = fopen("business_list.csv", "r");
while (($data = fgets($fp,4096)) !== FALSE) 
{
	$data=str_replace('","',"|",$data);
	$data=str_replace('"',"",$data);
	$data=str_replace("'","",$data);
	$data_elements=explode("|",$data);

       // the data elements below are for user first name and last name which is field 45 and 49

	$sql="select userdb_id from ".$sqltablepref."userdb where userdb_user_name like '".$data_elements[45].$data_elements[49]."'";
	$select=mysql_query($sql);
	$rows=mysql_num_rows($select);
	$row=mysql_fetch_array($select);
	if($rows==1)
	{
	continue;
	}
	$user_id=0;
	$sql="select max(userdb_id) from ".$sqltablepref."userdb";
	$select=mysql_query($sql);
	while($row=mysql_fetch_array($select))
	$user_id=$row[0]+1;
	if(strlen($data_elements[45]) && strlen($data_elements[49]))
	{
	$sql= "INSERT INTO " . $myconfig['table_prefix'] . "userdb VALUES 
			(".$user_id.",
			'".$data_elements[45].$data_elements[49]."',
			'".$data_elements[53]."',
			'".$data_elements[45]."',
			'".$data_elements[49]."',
			'Contact Jared Ritchey For Details',
			'no',
			'no',
			'no',
			'no',
			'no',
			'no',
			'2008-07-16',
			'no',
			'no',
			'2008-07-16 11:20:49',
			1,
			'no',
			'no',
			'no',
			'yes',
			'no',
			-1,
			'no',
			'no',
			'no',
			'no',
			'no',
			'no',
			'no'
			)";
	mysql_query($sql);
	$specified_user_id=0;
	$sql="select userdb_id from ".$sqltablepref."userdb where userdb_user_name like '".$data_elements[45].$data_elements[49]."'";
	$select=mysql_query($sql);
	while($row=mysql_fetch_array($select))
	$specified_user_id=$row[0];
	foreach($agent_fields as $field=>$num)
		{
		$sql = "INSERT INTO  ".$sqltablepref."userdbelements SET
			userdbelements_field_name = '".$field."',
			userdbelements_field_value = '".$data_elements[$num]."',
			userdb_id  =".$specified_user_id;
		mysql_query($sql);
		}
	}
}
fclose($fp);
echo("business directory added"."<br />"); 

 

What happens is that when I run this script to update my directory it includes the very top row of the text file which is the column headers as a record. I dont want the top row included as a record.

Link to comment
https://forums.phpfreaks.com/topic/137369-help-with-arrays/
Share on other sites

Just rewrite the array without the first 3 elements (the first row).

<?php
$elements_copy = $elements;
for ($i = 2; $i < count($elements); $i++)
    $elements[$i-2] = $elements_copy[$i];
?>

Or something like that, I didn't exactly look through the whole code, I'm assuming you'd change the variables to whatever suits you.

Link to comment
https://forums.phpfreaks.com/topic/137369-help-with-arrays/#findComment-717729
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.