How to create different variables in the same while loop

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1696 Views - Last Post: 08 June 2010 - 10:21 AM Rate Topic: -----

#1 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

How to create different variables in the same while loop

Posted 07 June 2010 - 11:42 AM

Hey guys.

Trying to work out how best to output different variables in my while-loop. This is what I have so far:

	
<?php			
$i	=	1;
				
while($att_count < 4)
{
?>
<p>#<?php echo $i ?>:
<input type="file" name="<?php echo "pic" .$i ?>" /></p>
<?php
$att_count++;
$i++;
?>



The point here is obviously to change the $_POST (or rather $_FILES, as in this case) value for the file-input-box every time the while loop runs through, but this does not work.

Any suggestions?

This post has been edited by Precaseptica: 07 June 2010 - 11:43 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to create different variables in the same while loop

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,036
  • Joined: 08-August 08

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 11:57 AM

That hurts my eyes! Why not do something simpler, like this?
for($att_count = 0; $att_count < 4; $att_count++)
{
	echo '<p>#'.$att_count.'<input type="file" name="pic'.$att_count.' /></p>';
}


Was This Post Helpful? 0
  • +
  • -

#3 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:15 PM

You can do many things in the different parts of a for loop. For example:

for ($a = 0, $b = 10; $a < $b; ++$a, --$B)/>
{
   print "a: $a - b: $b\n";
}


Was This Post Helpful? 0
  • +
  • -

#4 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:18 PM

View PostCTphpnwb, on 07 June 2010 - 10:57 AM, said:

That hurts my eyes! Why not do something simpler, like this?
for($att_count = 0; $att_count < 4; $att_count++)
{
	echo '<p>#'.$att_count.'<input type="file" name="pic'.$att_count.' /></p>';
}



Because my variable $att_count is not 0 from the start, but a value between 1 and 4, because I don't like echoing out forms within php tags, and because it still doesn't solve the issue. Your name="pic'.$att_count'" will attempt to do the same thing I'm doing, and it won't work.

There should be no difference to that and [name="x<?php echo $incremented_value ?>"]
Was This Post Helpful? 0
  • +
  • -

#5 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:24 PM

View Postmahcuz, on 07 June 2010 - 11:15 AM, said:

You can do many things in the different parts of a for loop. For example:

for ($a = 0, $b = 10; $a < $b; ++$a, --$B)/>
{
   print "a: $a - b: $b\n";
}



Indeed, but it's a name-value within a form I need to create as a posted value, different from the last time it ran through, and I can't quite figure out how your advice should help me do that.

What I have is, let's say 3 different pictures that will be uploaded, and the name="whatever" within the form needs to have a new name for each time it runs through the loop. So with that in mind, I have to crack the issue of how to make the name be different in each loop. I guess I could just make it with numbers, I don't know.
Was This Post Helpful? 0
  • +
  • -

#6 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:32 PM

View PostPrecaseptica, on 07 June 2010 - 07:24 PM, said:

View Postmahcuz, on 07 June 2010 - 11:15 AM, said:

You can do many things in the different parts of a for loop. For example:

for ($a = 0, $b = 10; $a < $b; ++$a, --$B)/>
{
   print "a: $a - b: $b\n";
}



Indeed, but it's a name-value within a form I need to create as a posted value, different from the last time it ran through, and I can't quite figure out how your advice should help me do that.

What I have is, let's say 3 different pictures that will be uploaded, and the name="whatever" within the form needs to have a new name for each time it runs through the loop. So with that in mind, I have to crack the issue of how to make the name be different in each loop. I guess I could just make it with numbers, I don't know.


I guess you want the html attribute name = "" to be different for every image ?
Yould add them in an array

$img_names = array('test img 1', 'test img 2');
for($i = 1; $i < $numberOfImages; $i++) {
    echo '<img src="img'.$i.'.jpg" name="'. $img_names[$i-1]. '" />';
}



Is this something you mean ?

This post has been edited by webpeater: 07 June 2010 - 12:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:44 PM

View Postwebpeater, on 07 June 2010 - 11:32 AM, said:

I guess you want the html attribute name = "" to be different for every image ?
Yould add them in an array

$img_names = array('test img 1', 'test img 2');
for($i = 1; $i < $numberOfImages; $i++) {
    echo '<img src="img'.$i.'.jpg" name="'. $img_names[$i-1]. '" />';
}



Is this something you mean ?


Right, this seems like it would work. But how do I call the values out with $_FILES?

$_FILES['img_names'][0] ?
Was This Post Helpful? 0
  • +
  • -

#8 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:51 PM

View PostPrecaseptica, on 07 June 2010 - 07:44 PM, said:

View Postwebpeater, on 07 June 2010 - 11:32 AM, said:

I guess you want the html attribute name = "" to be different for every image ?
Yould add them in an array

$img_names = array('test img 1', 'test img 2');
for($i = 1; $i < $numberOfImages; $i++) {
    echo '<img src="img'.$i.'.jpg" name="'. $img_names[$i-1]. '" />';
}



Is this something you mean ?


Right, this seems like it would work. But how do I call the values out with $_FILES?

$_FILES['img_names'][0] ?


The filename of an uploaded file is always $_FILES['name_of_input_where_file_uploaded']['name']
I don't know how you are storing your files ?

Give us the code you are using now

This post has been edited by webpeater: 07 June 2010 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 12:57 PM

View Postwebpeater, on 07 June 2010 - 11:51 AM, said:

View PostPrecaseptica, on 07 June 2010 - 07:44 PM, said:

View Postwebpeater, on 07 June 2010 - 11:32 AM, said:

I guess you want the html attribute name = "" to be different for every image ?
Yould add them in an array

$img_names = array('test img 1', 'test img 2');
for($i = 1; $i < $numberOfImages; $i++) {
    echo '<img src="img'.$i.'.jpg" name="'. $img_names[$i-1]. '" />';
}



Is this something you mean ?


Right, this seems like it would work. But how do I call the values out with $_FILES?

$_FILES['img_names'][0] ?


The filename of an uploaded file is always $_FILES['name_of_input_where_file_uploaded']['name']
I don't know how you are storing your files ?

Give us the code you are using now


I don't see how that would help, but sure:

<input type="file" name="<?php echo $img[$i-1] ?>" />


Calling it with:

$pic1 = $_FILES['img'];


Try and examplify how you would call your own name-tag, with your prior example img_names[$i-1]
Was This Post Helpful? 0
  • +
  • -

#10 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 01:05 PM

View PostPrecaseptica, on 07 June 2010 - 07:57 PM, said:

I don't see how that would help, but sure:

<input type="file" name="<?php echo $img[$i-1] ?>" />


Calling it with:

$pic1 = $_FILES['img'];


Try and examplify how you would call your own name-tag, with your prior example img_names[$i-1]


You can not call one image in that way.
You have $_FILES['img1'], $_FILES['img2'], ... depends on how many inputs you display and how many are filled in.

But can you explain to us, what you are trying to do ?

You want to upload images, and than output all the names of these images ?
Was This Post Helpful? 0
  • +
  • -

#11 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 01:38 PM

View Postwebpeater, on 07 June 2010 - 12:05 PM, said:

View PostPrecaseptica, on 07 June 2010 - 07:57 PM, said:

I don't see how that would help, but sure:

<input type="file" name="<?php echo $img[$i-1] ?>" />


Calling it with:

$pic1 = $_FILES['img'];


Try and examplify how you would call your own name-tag, with your prior example img_names[$i-1]


You can not call one image in that way.
You have $_FILES['img1'], $_FILES['img2'], ... depends on how many inputs you display and how many are filled in.

But can you explain to us, what you are trying to do ?

You want to upload images, and than output all the names of these images ?


Well, what I did was I created a forum, with the option to attach images to posts. Now that's working fine, but it's a bit tricker to add the same functionality to the "edit post" feature. First of all, the amount of images you can upload has to be restricted by how many are already attached to that post. This is the reason for the $acc_count variable.
So what I'm doing with this exact piece is to try and create a loop, restricted by how many images are already attached to the post, running as many times as there are spots available for pictures, and then posting the pictures uploaded into the db, storing them in an upload folder and thereby making them a part of the shown material in the thread.

I think I see how you're doing it now, though. So I will try and tinker a bit with what information I have from here, and make it work. The code is MASSIVE because of lack of structuring on my behalf, so I wouldnt want to expose you to all of it.
Was This Post Helpful? 0
  • +
  • -

#12 webpeater  Icon User is offline

  • D.I.C Head

Reputation: 26
  • View blog
  • Posts: 95
  • Joined: 04-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 01:51 PM

Go a head and play with the info you got.

But you can use the $acc_count in a different way.
You did
while($acc_count < $some_number {

$acc_count++;
}

But you could make it easier:
for($i = 1; $i <= ($some_number-$acc_count); $i++) { ... }

In that way you could loop easy over input1, input2, ... like you read in other posts.
Was This Post Helpful? 0
  • +
  • -

#13 mahcuz  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 213
  • Joined: 03-June 10

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 02:27 PM

BTW, naming your file inputs numerically isn't the best way to go about this. Instead, name them all the same and append "[]" to them. When you do that, PHP will treat them as an array. You can then:
foreach($_FILES as $key => $file) {
    print $file['name']; // etc
}


Was This Post Helpful? 0
  • +
  • -

#14 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 02:41 PM

View Postwebpeater, on 07 June 2010 - 12:51 PM, said:

Go a head and play with the info you got.

But you can use the $acc_count in a different way.
You did
while($acc_count < $some_number {

$acc_count++;
}

But you could make it easier:
for($i = 1; $i <= ($some_number-$acc_count); $i++) { ... }

In that way you could loop easy over input1, input2, ... like you read in other posts.


Yeah, this seemed smarter, so I followed your advice.

My code is still not working though. I'm using:

<input type="file" name="<?php echo $img[$i-1] ?>" />


And calling with:

$pic1		=	$_FILES['img0'];


But it's returning nothing when I killed the script with die($pic1)
Was This Post Helpful? 0
  • +
  • -

#15 Precaseptica  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 26-December 09

Re: How to create different variables in the same while loop

Posted 07 June 2010 - 02:49 PM

View Postmahcuz, on 07 June 2010 - 01:27 PM, said:

BTW, naming your file inputs numerically isn't the best way to go about this. Instead, name them all the same and append "[]" to them. When you do that, PHP will treat them as an array. You can then:
foreach($_FILES as $key => $file) {
    print $file['name']; // etc
}



Agreed.

So I changed it to:

<input type="file" name="pic[]" />


Calling it with:

$pic1		=	$_FILES['pic'];


Still doesn't output anything though.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2