Question:
Problem with PHP WHILE LOOP and LIMITING characters in variables.?
peter s
2007-01-01 09:54:06 UTC
Ok this is the small script that limits and cuts the amount of characters in variables to 12 and echos it. There works fine, but when i try and put this into a while loop on the second loop it will say:
Fatal error: Cannot redeclare sub() (previously declared..
Because it was declared in the first loop. But i need to put it in a loop so that each row['COLOR'] from my database is automatically limited to 12 letters.

How can i do this? To change it so it works in a while loop. Thanx. Code below:



$string = $row['COLOR'];
function sub($string)
{
$Length = strlen($string);
if($Length > 12)
$string = substr($string, 0, 12);
return($string);
}
echo sub($string);
Three answers:
worm22
2007-01-01 12:43:21 UTC
function sub($string)

{

$Length = strlen($string);

if($Length > 12)

$string = substr($string, 0, 12);

return($string);

}



while ($row = mysql_fetch_array($resultset)) {

echo sub($row['COLOR']);

}



-------



Basically the function should be declared outside of the loop and then called from within it. Generally I declare all of my functions at the top of the script or in a separate file that will be included at the top of the main application script.
professorminh
2007-01-01 10:05:59 UTC
where is the loop?



do like this?



$string = $row['COLOR'];

function sub($string)

{

$Length = strlen($string);

if($Length > 12)

$string = substr($string, 0, 12);

return($string);

}



for(i=0; i < numberOfRow; i++) {

$string = $row['COLOR'];

echo sub($string);

}
kroner
2016-10-06 11:12:28 UTC
Hehe, this is an common one... Riza Hawkeye from Fullmetal Alchemist Brotherhood. one in each and every of my well known fictional characters in maximum situations. with the aid of fact i like her difficult, strict, do no longer-care-lots approach and on an analogous the actual shown fact that she has one in each and every of those mushy spot for the Colonel. one in each and every of maximum unswerving characters you ought to fulfill who could do something for the guy she cares for many, and an extremely good sniper experienced at exceptionally lots each and every thing. different characters: - The Elric Brothers, Winry Rockbell (FMAB), Ino Yamanaka, Tsunade Senju, Minato Namikaze and Naruto Uzumaki of path (Naruto - if yellow hair counts!), Mami Tomoe (Madoka Magica), Android 18 (DBZ), Katsuya Jounouchi (Yu-Gi-Oh)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...