FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » parameters with numbers
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: parameters with numbers [message #178854 is a reply to message #178853] Tue, 14 August 2012 21:27 Go to previous messageGo to previous message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma:
Member
houghi wrote:
> I have something like the following:
>
> <?php
> include_once('connect.php');
> $ncast_1 = 'nm0001147';
> $ncast_2 = 'nm0518718';
> $ncast_3 = 'nm0001212';
> $ncast_4 = 'nm0004825';
> for ($i = 1; $i <= 4; $i++) {
> $sql = "SELECT * FROM cast WHERE nconst LIKE '$ncast_1'";
> $result = mysql_query($sql) or die(mysql_error());
> $row = mysql_fetch_array( $result );
> $cast_[$i] = $row['name'];
> echo "$cast_[$i] $i <br>";
> }
> mysql_close($conn);
> ?>
>
> This works as expected, but not as I want it to work. The $ncast_1 after
> the LIKE in the $sql should vary with the value of $i.
>
> I have tried using various things, like $ncast_[$i] which did not work.
> At this moment I am in the state of typing almost random characters and
> quotes and escapes.
>
> I have something working as it is four only different parameters and I
> just repeated the code, instead of using a loop. However I would like to
> learn and have no idea what to type into google to do the search.

Indeed it's possible to construct the name of a variable dynamically.
Just replace the first line of the for loop's body with:

$ncast = "ncast_$i";
$sql = "SELECT * FROM cast WHERE nconst LIKE '$$ncast'";

But I would recommend using an array instead as you have done with
$cast_; see <http://php.net/manual/en/language.types.array.php> or
google for "php array".

HTH
--
Christoph M. Becker
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: PHP Debugger which can set breakpoint and view variables on watch window
Next Topic: PHPUnit documentation
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 25 03:42:11 GMT 2024

Total time taken to generate the page: 0.04899 seconds