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
Share on other sites

$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);

for($i=0;$i<count($AL);$i++) {
$AL[$i] = implode("", $AL[$i]);
}

 

Something like that maybe?

Link to comment
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
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
Share on other sites

Your code makes little sense. You are trying to define a string using implode, but the second argument your passing to implode is this same variable you haven't defined yet.

 

ps: Variable variables are slow, use an arrays instead.

Link to comment
Share on other sites

Because your not providing enough information and the code examples are so far from working code that they make no sense (not to mention the fact that your variable names do NOTHING to help us know what data they store).

Link to comment
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
Share on other sites

And I have already explained the reason for the error. Your second argument to implode is the same variable you are trying to create. Hence, it does not exist when you pass it to implode.

 

Your code and your approach to this problem both have some serious floors in logic.

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.