Page 1 of 1
My Loop Problems
Posted: Fri Jul 17, 2009 4:30 pm
by GlennCurtis2009
Hi Guys,
I am hoping someone can help me with this. I have two loops, one with one. The while loop is not a problem works fine, gets the data form the DB and i can print each row. Although not all rows are used, there is a set varable '$numberof' which holds the number used. Therefore my logic come up with what i got below.
The problem is that allthough print ("$row[Item01]"); will work and print the 1st row, when i replace the 1 with the $i count my script does not want to display anything. It will print $i but anything i do does not seem to want to print the number of rows needed for each line, ect 3, 4, 6 or just 1 or 2, depending on what is set by $numberof.
Hope someone can help
Thanks Glenn Curtis
Code: Select all
while ($row = mysql_fetch_array($BillingData)){
for ($i=1; $i<=$NumberOf; $i++) {
print ("This is i : $i <br><br>");
print ("Row Data: $row[Item0$i]<br>");
}
} // End of while loop
Re: My Loop Problems
Posted: Fri Jul 17, 2009 4:49 pm
by requinix
There are some database normalization issues here but implementing those changes will probably never happen...
Code: Select all
print ("Row Data: $row[Item0$i]<br>");
That results in a parse error. Using that kind of syntax just isn't valid and PHP will complain if you try.
Instead
look in the manual to see what is allowed.
Code: Select all
print ("Row Data: {$row["Item0$i"]}<br>");
Re: My Loop Problems
Posted: Fri Jul 17, 2009 4:51 pm
by McInfo
@GlennCurtis2009: I'm not sure what the script is attempting to accomplish. Does this demonstrate what you are trying to do?
Code: Select all
<?php
header('Content-Type: text/plain');
$rows = array
( 0 => array
( 'Item01' => 'A'
, 'Item02' => 'B'
, 'Item03' => 'C'
)
, 1 => array
( 'Item01' => 'D'
, 'Item02' => 'E'
, 'Item03' => 'F'
)
, 2 => array
( 'Item01' => 'G'
, 'Item02' => 'H'
, 'Item03' => 'I'
)
, 3 => array
( 'Item01' => 'J'
, 'Item02' => 'K'
, 'Item03' => 'L'
)
);
foreach ($rows as $row) {
foreach ($row as $field => $value) {
echo $value;
}
echo "\n";
}
?>
Edit: This post was recovered from search engine cache.
Re: My Loop Problems
Posted: Sat Jul 18, 2009 1:08 am
by GlennCurtis2009
Hi guys,
Iam sorry dude but your use of qute marks within the arrray marker did not work at all.
Iam I not sure what your array code is trying to do.
Ok I try to explain again.
I have a DB with up to 20 fileds within it, Item01 to Item 20, but not all of them will be used, I have a count set, $numberof which holds the number of fileds used within each record set, e.g. 1, 3, 5 or 20. Alll I want to do is print Item01 to Item20 but only when they are used and not null.
Hope that clears things up
Thanks
Glenn Curtis.
Re: My Loop Problems
Posted: Sat Jul 18, 2009 1:42 am
by requinix
GlennCurtis2009 wrote:Iam sorry dude but your use of qute marks within the arrray marker did not work at all.
Just making sure: you
did notice the {}s, right?
Re: My Loop Problems
Posted: Sat Jul 18, 2009 10:44 am
by GlennCurtis2009
Sorry mate,
No I didn't see the {}, have just tryed it, needs a bit of cleaning up but seems to work fine.
Thanks
Glenn
Re: My Loop Problems
Posted: Sat Jul 18, 2009 12:12 pm
by McInfo
GlennCurtis2009 wrote: not sure what your array code is trying to do.
The array represents data returned by a database query. The code loops through each row and, within each row, loops through the fields and displays the value.
GlennCurtis2009 wrote:I have a DB with up to 20 [fields] within it, Item01 to Item 20, but not all of them will be used
That indicates that the table structure is not normalized, as tasairis mentioned.
Assume you have a table like this one that holds numbers and multiple colors for each number.
Code: Select all
+----+-------+--------+---------+----------+--------+
| id | name | color1 | color2 | color3 | color4 |
+----+-------+--------+---------+----------+--------+
| 1 | One | Red | | | |
| 2 | Two | Green | Greener | Greenest | |
| 3 | Three | Blue | Bluer | | |
+----+-------+--------+---------+----------+--------+
If your business rules state that each number should have four or fewer colors, this table might be acceptable. However, if adding another color requires the addition of another field, this is not the appropriate table structure. Fields do not expand, rows do.
To normalize the table, it needs to be split into two tables, one for each entity (number and color).
`number` table
Code: Select all
+------+--------+
| n_id | n_name |
+------+--------+
| 1 | One |
| 2 | Two |
| 3 | Three |
+------+--------+
`color` table
Code: Select all
+------+----------+
| n_id | c_color |
+------+----------+
| 1 | Red |
| 2 | Green |
| 2 | Greener |
| 2 | Greenest |
| 3 | Blue |
| 3 | Bluer |
+------+----------+
Edit: This post was recovered from search engine cache.
Re: My Loop Problems
Posted: Sat Jul 18, 2009 1:08 pm
by McInfo
If you are not convinced that normalization is the way to go, try this example.
This is the `notnormal` table.
Code: Select all
DESCRIBE `notnormal`;
# +--------+----------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +--------+----------------------+------+-----+---------+----------------+
# | id | smallint(3) unsigned | NO | PRI | | auto_increment |
# | name | varchar(20) | NO | | | |
# | item01 | varchar(10) | YES | | | |
# | item02 | varchar(10) | YES | | | |
# | item03 | varchar(10) | YES | | | |
# | item04 | varchar(10) | YES | | | |
# +--------+----------------------+------+-----+---------+----------------+
SELECT * FROM `notnormal`;
# +----+--------+-----------+--------+--------+--------+
# | id | name | item01 | item02 | item03 | item04 |
# +----+--------+-----------+--------+--------+--------+
# | 1 | Bronze | Sculpture | Sword | NULL | NULL |
# | 2 | Gold | Coin | NULL | NULL | NULL |
# | 3 | Silver | NULL | Spoon | NULL | NULL |
# +----+--------+-----------+--------+--------+--------+
This script lists the non-null items.
Code: Select all
<?php
header('Content-Type: text/plain');
$dbc = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $dbc);
$query = 'SELECT * FROM `notnormal`';
$result = mysql_query($query, $dbc);
while ($row = mysql_fetch_assoc($result)) {
echo 'id: ', $row['id'], "\n";
echo 'name: ', $row['name'], "\n";
// There are four item fields
for ($i = 1; $i <= 4; $i++) {
// Makes field name like item01, item02, etc.
$field = sprintf('item%02d', $i);
// Ignores nulls
if (!is_null($row[$field])) {
// Echoes field name and value
echo $field, ': ', $row[$field], "\n";
}
}
echo '----------', "\n";
}
mysql_free_result($result);
mysql_close($dbc);
?>
Code: Select all
id: 1
name: Bronze
item01: Sculpture
item02: Sword
----------
id: 2
name: Gold
item01: Coin
----------
id: 3
name: Silver
item02: Spoon
----------
Edit: This post was recovered from search engine cache.