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
Switch to threaded view of this topic Create a new topic Submit Reply
parameters with numbers [message #178853] Tue, 14 August 2012 20:16 Go to next message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
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.

houghi
--
You are about to enter another dimension, a dimension not only of
sight and sound but of mind. A journey into a wondrous land of
imagination. Next stop, Usenet!
Re: parameters with numbers [message #178854 is a reply to message #178853] Tue, 14 August 2012 21:27 Go to previous messageGo to next message
Christoph Becker is currently offline  Christoph Becker
Messages: 91
Registered: June 2012
Karma: 0
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
Re: parameters with numbers [message #178855 is a reply to message #178853] Tue, 14 August 2012 22:15 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Tue, 14 Aug 2012 22:16:55 +0200, 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';

You could declare these as an array:

$ncast = array('nm0001147','nm0518718','nm0001212','nm0004825');

> for ($i = 1; $i <= 4; $i++) {

In an array, your indexes start from 0 by default:

for ($i = 0; $i < 4; $i++) { // this gives 0, 1, 2, 3

> $sql = "SELECT * FROM cast WHERE nconst LIKE '$ncast_1'";

Then in this line, you could use:

$sql = "SELECT * FROM cast WHERE nconst LIKE '{$ncast[$i]}'";

Note that the {} are essential when you want to reference an array inside
a php string, which in this case is everything between the " characters!

> $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.

Rgds

Denis McMahon
Re: parameters with numbers [message #178856 is a reply to message #178855] Tue, 14 August 2012 22:31 Go to previous message
houghi is currently offline  houghi
Messages: 45
Registered: September 2011
Karma: 0
Member
Denis McMahon wrote:
<Snip>

Thanks both both the reply. Learned a lot from that. Made things clear
and possible to solve other things I will struggle with in the future.

houghi
--
You are about to enter another dimension, a dimension not only of
sight and sound but of mind. A journey into a wondrous land of
imagination. Next stop, Usenet!
  Switch to threaded view of this topic Create a new topic Submit Reply
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: Thu Nov 21 22:20:48 GMT 2024

Total time taken to generate the page: 0.03048 seconds