Jump to content

How do I increment a variable???


agf2309

Recommended Posts

Hi everyone, 

 

I am trying to build a script that will take data from a text file and get a number of filenames from the text.  I've managed to get it to do this.  I then want to UPDATE the filenames into my table.  The filenames are image names.  So for example each row in the table might have multiple images in the "allimages" field.  So far I have amanged to get these individual filenames and now I want to update them to fields called "pic1", "pic2" etc through "pic15".

 

What I am trying to do is to get the script to name the column that the filename goes into.  e.g. 1st pic goes into "pic1" etc.  I have set a variable for $col="pic" and I want to create a variable called $colno=$col[$i];  where $i increments dependent on the no of pictures in the text file.

 

This is probably as clear as mud.  I have inserted my code so far below, Maybe someone can help me or point me in the right direction.  Thanks.  Tony

 


<?php do { // Begin PHP
  if($row_images['allimages']!=""){
  $data = preg_split("/;/", $row_images['allimages'], 15, PREG_SPLIT_NO_EMPTY);// get each URL from DB
	for($c=0; $c < count($data); $c++){// for statement 1
	$url=$data[$c];
	}// end of for statement 1
	}// end of opening if statement
	foreach($data as $key => $line){// forech statement 1 - explode data array to get file name
  $pics = explode("http://82.195.133.221/vehicleimages/used/", $line, 15);

for($i = 1; $i < count($pics); $i++){//for statement 2
foreach($pics as $key => $value){//foreach statement 2
$pic=$pics[$i];
$stockno=$row_images['stockno'];
for($a=0; $a<=count($data); $a=$a+1)
{
$col="pic";
$colno=$col.$a;
}
}// end of foreach statement 2
echo $colno. " = ". $pic.$stockno."(".count($data).")<br />"; 
} // end of for statement 2
}// end of foreach statement 1
}// end cury brace for start of PHP


          while ($row_images = mysql_fetch_assoc($images)); ?> 

Link to comment
Share on other sites

Hold up. You have columns in your table named pic1 through pic15? That's a bad design. It's hard to maintain and hard to change. It's also hard to search with anything other than the most basic search.

 

I'd suggest you alter your database design to have a separate table for the pictures, linked to your main table by a unique ID. You might like to read up about database normalization and have a read of this tutorial: http://www.phpfreaks.com/tutorial/data-joins-unions

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.