Hello guys , 
             I am getting crazy with this html thing , i just don't know why its not working as expected. 
            Basically i need to display php variable in html 
            Let see the code....




   Suppose i have this



    <?php
            for($x = 0;$x <count($_FILES['image']['name']);$x++) {
            $url = something; 
            }
            //The value of $url changes at every instance of loop(i.e for each value of $x)
    ?>





Now i want to output each instance of $url in html below 

    <?php for($x = 0;$x <count($_FILES['image']['name']);$x++) : ?>
    <center><td><input type="text" size="50" value="<?php echo $url ?>"/></td><center>
    <?php endfor; ?>



The problem with this code is that **only the FIRST** value of `$url` get output. say the first value of $url is 
`$url = 'http://www.daniweb.com'`
I get output 



    <center><td><input type="text" size="50" value="http://www.daniweb.com"/></td><center>




This is good but now imagine the value of $url become `http://www.daniweb.com` and `http://www.google.com` during the loop
I need to get an output as follows 



    <center><td><input type="text" size="50" value="http://www.daniweb.com"/></td><center>
    <center><td><input type="text" size="50" value="http://www.google.com"/></td><center>

    With my present code i get 
    <center><td><input type="text" size="50" value="http://www.daniweb.com"/></td><center>
    <center><td><input type="text" size="50" value="http://www.daniweb.com"/></td><center>   


Thanks for the help guys 

Sorry for putting code tags everywhere but i keep getting

The code snippet in your post is formatted incorrectly. Please use the Code button in the editor toolbar when posting whitespace-sensitive text or curly braces.

in line 15:
$url[] = something;

in line 27:
<center><td><input type="text" size="50" value="<?php echo $url[$x] ?>"/></td><center>

I hope it fixes it

what is the value of $url? what is something?

$url = something; 

of course your $url only prints one values, because it is overwritten everytime it loops, it's called destructive readin, use array to store each values or concatenate it

vaultweller123 is exactly right.

If you declare a variable:
$variable = "value";
...and then you declare the same variable with a different value:
$variable = "another value";
...the variable holds the second value and the first value is lost.
It is the same in your loop, your variable '$url' can only hold one value at a time, so you need to process the value each time through the control loop, for example:

for($x = 0;$x <count($_FILES['image']['name']);$x++){
    //here the value of $url is echoed along with the other HTML.
    echo "<center><td><input type='text' size='50' value=\"";
    echo $url;
    echo "\"/></td><center>";
}

or you can store the value in an array and work with it afterwards:

$urls = new array();
for($x = 0;$x <count($_FILES['image']['name']);$x++){
    $urls[] = $url; //here the value of $url is saved in the $urls array. 
}

then user a foreach, for instance:

foreach($urls as $url){ //no need to count, when there are no more urls, the loop stops.
    echo "<center><td><input type='text' size='50' value=\"";
    echo $url;
    echo "\"/></td><center>";
}

Good luck :D

@adam.adamski.96155
I was current your first solution ...however now i feel that multiple echo don't look so good , that why i created this thread.

The foreach method is good but i don't have just $url to output here , there are around 6 variables in all in my project .
Can any of you suggest a better method?

I split the echo line into three for readability, it does not have to be that way:
echo "<center><td><input type='text' size='50' value='$url' /></td><center>";
Your original loop had a strange syntax:
<?php for($x = 0;$x <count($_FILES['image']['name']);$x++) : ?>
<?php endfor; ?>
If I was writing the same loop it would look like the following:

<?php
for($x = 0;$x <count($_FILES['image']['name']);$x++){
    echo "<center><td><input type='text' size='50' value='$url' /></td><center>\n";
}
?>

You mention that your loop only produces one line, but what is the value of count($_FILES['image']['name'])?

@Adam i mean i was using the 1st method you told me in this thread before .The script was working(i.e it was producing all lines).However i think its better to seperate the html with my main php.That's why i created this thread to get more options
Is there any better alternative to foreach so that i can echo various variable(in e.g its just url)

count($_FILES['image']['name'])?

is the number of files a user has selected for upload.this line is good and shouldn't be change

Either way you will be writing HTML with PHP in it, or PHP with HTML in it.
You have the option to open a for loop and then close the PHP block...
<?php for($i=0;$i<$count;$i++){?>
...now you would put standard HTML with PHP variables echoed:
<center><td><input type="text" size="50" value="<?php echo $url ?>"/></td><center>
and then open the PHP block to close the for loop:
<?php } // close brace ?>
which was your original method with a slightly different syntax. Or you can use PHP to produce the HTML and echo the variables:

<?php for($i=0;$i<$count;$i++){
    echo "<center><td><input type='text' size='50' value='$url' /></td><center>\n";
} ?>

If you are looking for further separation, you could put the HTML producing sections of your PHP code into a function that is separated from your main PHP code, or even in another file, but somewhere there will be a mixture.

Its not working dude
I think i haven't expressed myself clear enough

My upload.php has currently 1 block of php code
1st block is

<?php
            for($x = 0;$x <count($_FILES['image']['name']);$x++) {
            $url = something; 
            }
            //The value of $url changes at every instance of loop(i.e for each value of $x)
            echo "<center><td><input type=\"text\" size=\"50\"value=\"$url\"/></td></center>";
    ?>

With this code , every instance of the $url get display.This is what i want.
Here is why i created this thread
However now i want to create a second block of php in upload.php that will STILL display every instance of $url.
This way one block would be dedicated to the displaying to variable within html tags
Second block should be something like this

<center><td><input type="text" size="50" value="<?php echo $_view_url?>"/></td><center>

echo "<center><td><input type=\"text\" size=\"50\" value=\"$modifyurl\"/></td></center>";
Hope i get a solution that works.I tried every possible ways as far as i know to achieve this but the result is that the same variable is displayed 2 times .

If I understand you correctly you want to echo more than one line of HTML each iteration of the foreach loop. You can either (a)make this happen within your existing loop, (b)repeat your existing loop and do something different in it, or (c)store the necessary variables in an array and run a second loop:

(a) A single loop that does all:

for($x = 0;$x <count($_FILES['image']['name']);$x++) {
$url = something;
echo "<center><td><input type=\"text\" size=\"50\"value=\"$url\"/></td></center>";
echo "<center><td><input type=\"text\" size=\"50\"value=\"$modify_url\"/></td></center>";
echo "<center><td><input type=\"text\" size=\"50\"value=\"$remodify_url\"/></td></center>";
}

(b) Two loops with same condition, but different output:

for($x = 0;$x <count($_FILES['image']['name']);$x++) {
$url = something;
echo "<center><td><input type=\"text\" size=\"50\"value=\"$url\"/></td></center>";
}
for($x = 0;$x <count($_FILES['image']['name']);$x++) {
$modify_url = something;
echo "<center><td><input type=\"text\" size=\"50\"value=\"$modify_url\"/></td></center>";
}

(c) One loop to output HTML and store vars for use in second loop:

$loopArray = new array();
for($x = 0;$x <count($_FILES['image']['name']);$x++) {
echo "<center><td><input type=\"text\" size=\"50\"value=\"$url\"/></td></center>";
// Store other vars in array for second loop
$loopArray[$x] = $modified_url //if only single var or:
// An array of arrays:
$loopArray[$x] = array($modified_url, $remodified_url, $re_modified_re_urled);
}

then the second loop:

foreach($loopArray as $v){
    //single vars in array?
    $single_variable = $v;

    //arrays in array?
    foreach($v as $arr){ //nested foreach
    $modified_url = $arr[0];
    $remodified_url = $arr[1]; 
    $re_modified_re_urled = $arr[2];
    }
}

Make sense?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.