Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #6782 > unrolled thread

parameters with numbers

Started byhoughi <houghi@houghi.org.invalid>
First post2012-08-14 22:16 +0200
Last post2012-08-15 00:31 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  parameters with numbers houghi <houghi@houghi.org.invalid> - 2012-08-14 22:16 +0200
    Re: parameters with numbers Christoph Becker <cmbecker69@gmx.de> - 2012-08-14 23:27 +0200
    Re: parameters with numbers Denis McMahon <denismfmcmahon@gmail.com> - 2012-08-14 22:15 +0000
      Re: parameters with numbers houghi <houghi@houghi.org.invalid> - 2012-08-15 00:31 +0200

#6782 — parameters with numbers

Fromhoughi <houghi@houghi.org.invalid>
Date2012-08-14 22:16 +0200
Subjectparameters with numbers
Message-ID<slrnk2lchn.drm.houghi@penne.houghi>
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!

[toc] | [next] | [standalone]


#6783

FromChristoph Becker <cmbecker69@gmx.de>
Date2012-08-14 23:27 +0200
Message-ID<k0efqe$sea$1@speranza.aioe.org>
In reply to#6782
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

[toc] | [prev] | [next] | [standalone]


#6784

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-08-14 22:15 +0000
Message-ID<k0eimp$q25$2@dont-email.me>
In reply to#6782
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

[toc] | [prev] | [next] | [standalone]


#6785

Fromhoughi <houghi@houghi.org.invalid>
Date2012-08-15 00:31 +0200
Message-ID<slrnk2lkdh.drm.houghi@penne.houghi>
In reply to#6784
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!

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web