Re: [PHP] Looping problem?

2004-01-06 Thread joel boonstra
On Tue, Jan 06, 2004 at 05:33:41PM -0500, joel boonstra wrote:

> I would recommend not simply doing a select *, but rather specifying
> which columns you want.  That way, if your database schema changes
> (e.g., you add two more columns), your code won't break.

And, responding to myself, specifying your columns is also good practice
because MySQL makes no guarantees about the order that columns are
returned when you say "SELECT *".  Today, it may well be the order that
you specified when you created your table.  With the next upgrade, it
may be alphabetical order, or by column type, or some other order that
the software chooses.  If you specify column names in your SELECT
statement, you don't need to worry about it.  This is especially
important when you are using list() to assign variables based on their
position in your fetched array.

joel

-- 
[ joel boonstra | gospelcom.net ]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Looping problem?

2004-01-06 Thread joel boonstra
On Wed, Jan 07, 2004 at 09:17:35AM +1100, Martin Towell wrote:
> This is probably more like what you need.
> I can't see why you'd need to loop through your results using two different
> methods (while and for)
> 
> require 'database.php';
> $t_02 = "subnets";
> $sql_subs = mysql_query("SELECT * FROM $t_02",$db) or die(mysql_error());
> $num = mysql_num_rows($sql_subs);
> for ($z = 0; $z < $num; $z++)
> {
>   list($id, $sub, $msk, $dns01, $dns02, $rtrs, $rnge) =
> mysql_fetch_array($sql_subs);
>   $vlans[] = "subnet $subnetmask $msk {option domain-name-servers
> $dns01, $dns02;option routers $rtrs;range $rnge;}";
> }
> // Write everything out formated...
> for ($z = 0; $z < $num; $z++)
>   echo "$vlans[$z]\n";

No need to calculate the number of rows:

netmask $msk {option domain-name-servers
$dns01, $dns02;option routers $rtrs;range $rnge;}";
  print $tmp;
  $vlans[] = $tmp;
}
?>

I would recommend not simply doing a select *, but rather specifying
which columns you want.  That way, if your database schema changes
(e.g., you add two more columns), your code won't break.

joel

-- 
[ joel boonstra | gospelcom.net ]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Looping problem?

2004-01-06 Thread Martin Towell
This is probably more like what you need.
I can't see why you'd need to loop through your results using two different
methods (while and for)

require 'database.php';
$t_02 = "subnets";
$sql_subs = mysql_query("SELECT * FROM $t_02",$db) or die(mysql_error());
$num = mysql_num_rows($sql_subs);
for ($z = 0; $z < $num; $z++)
{
  list($id, $sub, $msk, $dns01, $dns02, $rtrs, $rnge) =
mysql_fetch_array($sql_subs);
  $vlans[] = "subnet $subnetmask $msk {option domain-name-servers
$dns01, $dns02;option routers $rtrs;range $rnge;}";
}
// Write everything out formated...
for ($z = 0; $z < $num; $z++)
  echo "$vlans[$z]\n";


Martin


> -Original Message-
> From: Jas [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, 7 January 2004 9:05 AM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Looping problem?
> 
> 
> require 'database.php';
> $t_02 = "subnets";
> $sql_subs = mysql_query("SELECT * FROM $t_02",$db)or 
> die(mysql_error());
> while(list($id,$sub,$msk,$dns01,$dns02,$rtrs,$rnge) 
> = mysql_fetch_array($sql_subs)) {
> $num = mysql_num_rows($sql_subs);
>for($z = 0; $z < $num; $z++) {
>   $vlans[] = "subnet $subnetmask $msk {option 
> domain-name-servers 
> $dns01, $dns02;option routers $rtrs;range $rnge;}"; }
> }
> // Write everything out formated...
> echo $vlans[$z]\n;
> 
> Right now it only pulling the last record???
> Jas
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Looping problem?

2004-01-06 Thread Brad Pauly
On Tue, 2004-01-06 at 15:04, Jas wrote:
> require 'database.php';
> $t_02 = "subnets";
> $sql_subs = mysql_query("SELECT * FROM $t_02",$db)or 
> die(mysql_error());while(list($id,$sub,$msk,$dns01,$dns02,$rtrs,$rnge) 
> = mysql_fetch_array($sql_subs)) {
> $num = mysql_num_rows($sql_subs);
>for($z = 0; $z < $num; $z++) {
>   $vlans[] = "subnet $subnetmask $msk {option domain-name-servers 
> $dns01, $dns02;option routers $rtrs;range $rnge;}"; }
> }
> // Write everything out formated...
> echo $vlans[$z]\n;
> 
> Right now it only pulling the last record???

You are only echo'ing the last record, $vlanz[$z]. You will need to loop
over $vlans again or put the echo line inside the for loop.

- Brad

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Looping problem?

2004-01-06 Thread Jas
require 'database.php';
$t_02 = "subnets";
$sql_subs = mysql_query("SELECT * FROM $t_02",$db)or 
die(mysql_error());	 while(list($id,$sub,$msk,$dns01,$dns02,$rtrs,$rnge) 
= mysql_fetch_array($sql_subs)) {
   $num = mysql_num_rows($sql_subs);
  for($z = 0; $z < $num; $z++) {
	$vlans[] = "subnet $subnetmask $msk {option domain-name-servers 
$dns01, $dns02;option routers $rtrs;range $rnge;}"; }
}
// Write everything out formated...
echo $vlans[$z]\n;

Right now it only pulling the last record???
Jas
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] looping problem?

2003-12-30 Thread Hitek
Shorter version of the script:

$line = file("fa.csv");
for($i=0;$i
$data = explode(",", $line[$i]);
echo "host $data[0] {\nhardware ethernet $data[1];\nfixed-address $data[2];\n}\n";
}
?>

At 08:10 AM 12/30/2003, Jas wrote:
Problem with looping over a CSV file (3 results for each line)?  Here is 
the CSV file contents...

MTPC-01,00:02:B3:A2:9D:ED,155.97.15.11
MTPC-02,00:02:B3:A2:B6:F4,155.97.15.12
MTPC-03,00:02:B3:A2:A1:A7,155.97.15.13
MTPC-04,00:02:B3:A2:07:F2,155.97.15.14
MTPC-05,00:02:B3:A2:B8:4D,155.97.15.15
Here is the script...

$row = 1;
$file = "fa.csv";
$id = fopen("$file","r");
  while($data = fgetcsv($id,100,",")) {
  $num = count($data);
  $row++;
for($c = 0; $c < $num; $c++) {
   echo "host $data[0] {\nhardware ethernet $data[1];\nfixed-address $data[2];\n}\n"; }
}
fclose($id);
?>

And this is the output...
(notice 3 results for the first line in the CSV file)
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
Any help is appreciated...
Jas
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] looping problem?

2003-12-30 Thread Mike Migurski
>Problem with looping over a CSV file (3 results for each line)?
>
>Here is the script...
>$row = 1;
>$file = "fa.csv";
>$id = fopen("$file","r");
>   while($data = fgetcsv($id,100,",")) {
>   $num = count($data);
>   $row++;
> for($c = 0; $c < $num; $c++) {
>echo "host $data[0] {\nhardware ethernet $data[1];/>\nfixed-address $data[2];\n}\n"; }
>   }
>fclose($id);
>?>

Remove the for-loop on the 8th line.

-
michal migurski- contact info and pgp key:
sf/cahttp://mike.teczno.com/contact.html

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] looping problem?

2003-12-30 Thread Jas
Problem with looping over a CSV file (3 results for each line)?  Here is 
the CSV file contents...

MTPC-01,00:02:B3:A2:9D:ED,155.97.15.11
MTPC-02,00:02:B3:A2:B6:F4,155.97.15.12
MTPC-03,00:02:B3:A2:A1:A7,155.97.15.13
MTPC-04,00:02:B3:A2:07:F2,155.97.15.14
MTPC-05,00:02:B3:A2:B8:4D,155.97.15.15
Here is the script...

$row = 1;
$file = "fa.csv";
$id = fopen("$file","r");
  while($data = fgetcsv($id,100,",")) {
  $num = count($data);
  $row++;
for($c = 0; $c < $num; $c++) {
   echo "host $data[0] {\nhardware ethernet $data[1];\nfixed-address $data[2];\n}\n"; }
	}
fclose($id);
?>

And this is the output...
(notice 3 results for the first line in the CSV file)
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
host MTPC-01 {
hardware ethernet 00:02:B3:A2:9D:ED;
fixed-address 155.97.15.11;
}
Any help is appreciated...
Jas
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php