Jump to content

for loop


hackalive

Recommended Posts

This code is meant to implode

	$qry_AL = "SELECT max(position) FROM `articles` WHERE `uri`='$uri'"; 
$result_AL= $mysqli->query($qry_AL, MYSQLI_STORE_RESULT);
$AL = mysqli_fetch_array($result_AL);
$AL = $AL['0'];
for($i=0;$i<=$AL;$i++) {
	$AL.$i = implode("", $AL.$i);
}

 

Basically what should happen is that $i is the maximum position value and then will implode all $AL# into just $AL#

so if max position is 9 it should loop through doing the implode for $AL0, $AL1 etc upto and including $AL9, the $i should reflect the moving up value till $i=9

 

any help would be so much appreciated, thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/197231-for-loop/
Share on other sites

You need to use variable variables. lol, short ex:

 

$variable = "test";
$name = "variable";
echo($$name);

// results as "test"

 

So:

 

<?php

$qry_AL = "SELECT max(position) FROM `articles` WHERE `uri`='$uri'"; 
$result_AL= $mysqli->query($qry_AL, MYSQLI_STORE_RESULT);
$AL = mysqli_fetch_array($result_AL);

$AL = $AL['0'];

// Just to show you it works
$name_array = array();

for($i=0;$i<=$AL;$i++) {
$name = $AL.$i;
$$name = implode("", $AL.$i);

// Just to show you it works
$name_array[] = $name;
}

// Just to show you it works
print_r($name_array);

?>

 

-CB-

Link to comment
https://forums.phpfreaks.com/topic/197231-for-loop/#findComment-1035291
Share on other sites

okay this works

 

	$qry_AL = "SELECT max(position) FROM `articles` WHERE `uri`='$uri'"; 
$result_AL= $mysqli->query($qry_AL, MYSQLI_STORE_RESULT);
$AL = mysqli_fetch_array($result_AL);
$AL = $AL['0'];
for($i=0;$i<=$AL;$i++) {
	${'AL'.$i} = implode("", ${'AL'.$i});
}
return $AL1;

 

but I get this message

Warning: implode() [function.implode]: Invalid arguments passed in "HIDDEN BY ME"\index.php on line 123

 

line 123 is

	$result_AL= $mysqli->query($qry_AL, MYSQLI_STORE_RESULT);

 

help is much appreciated, and thanks to all those who have helped so far

Link to comment
https://forums.phpfreaks.com/topic/197231-for-loop/#findComment-1035301
Share on other sites

okay,

in a tbl field I have (as an example) AAA:

<AL1><AL2><b><AL3>

 

furthermore in another table field I have BBB:

<AL1>How is it going?<AL2>Well<AL3>Thats great

 

I have code which works that replaces <AL1> etc from "AAA" with the relevant content from "BBB"

 

finally what happens is this

	$qry_AL = "SELECT max(position) FROM `articles` WHERE `uri`='$uri'"; 
$result_AL= $mysqli->query($qry_AL, MYSQLI_STORE_RESULT);
$AL = mysqli_fetch_array($result_AL);
$AL = $AL['0'];
for($i=0;$i<=$AL;$i++) {
	${'AL'.$i} = implode("", ${'AL'.$i});
}
return $PL1

 

this code finds the max(position) in a table (eg 5), what happens is it implodes $AL1 array into $AL1 or $AL2 array into $AL2 etc

but it is resulting in the already mentioned error

 

Link to comment
https://forums.phpfreaks.com/topic/197231-for-loop/#findComment-1035695
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.