Jump to content

Recommended Posts

Hi Everyone,

 

I am looking for some help with creating a loop in PHP. Basically my table has columns named Mon1, Mon2, Mon3 all the way up to 50. I have pasted my code below, but I don't think it's a good way I am doing it. I have thought about trying to use the code (below the old code)

                            <?php
                               $result = mysqli_query($mysqli, $query);

//while ($row = mysqli_fetch_array($result)) {
  //  echo $row[0] . ': ' . $row[1] . ' ' . $row[2];
  //  echo '<br />';
//}
while ($row = mysqli_fetch_array($result)) {
    echo $row['MON1'] . ' - ' . $row['MON2'] . ' - ' . $row['MON3']  . ' - ' . $row['MON4'] . ' - ' . $row['MON5'];
    echo '<br />';
	echo $row['TUE1'] . ' - ' . $row['TUE2'] . ' - ' . $row['TUE3']  . ' - ' . $row['TUE4'] . ' - ' . $row['TUE5'];
    echo '<br />';
	echo $row['WED1'] . ' - ' . $row['WED2'] . ' - ' . $row['WED3']  . ' - ' . $row['WED4'] . ' - ' . $row['WED5'];
    echo '<br />';
	echo $row['THU1'] . ' - ' . $row['THU2'] . ' - ' . $row['THU3']  . ' - ' . $row['THU4'] . ' - ' . $row['THU5'];
    echo '<br />';
	echo $row['FRI1'] . ' - ' . $row['FRI2'] . ' - ' . $row['FRI3']  . ' - ' . $row['FRI4'] . ' - ' . $row['FRI5'];
    echo '<br />';
}

mysqli_free_result($result);

mysqli_close($mysqli);
								?>
                            <?php
                               $result = mysqli_query($mysqli, $query);

$i = 1;
while ($1 < 50) {
while ($row = mysqli_fetch_array($result)) {
    echo $row['MON$i'];
}
}
mysqli_free_result($result);

mysqli_close($mysqli);
?>

Kind Regards,

 

Stuart

 

Link to comment
https://forums.phpfreaks.com/topic/301035-php-mysql-loop/
Share on other sites

the reason you are having a hard time writing code, or are writing a ton of code that only differs in a name or value, to deal with your data is because your database design is bad. any time you find yourself with database table columns, variable names, or associative array names that have numerical endings, it's a sign that you are doing something wrong while programming.

 

the point of databases is, each item (one, singular) of data is stored in its own row. you can then write simple queries and have simple code to retrieve any portion of the data you want. you also only store data that exists and wouldn't have 'slots' for each possible value.

 

your data apparently corresponds to dates. you would instead have columns for the date (a mysql DATE YYYY-MM-DD data type) of the data, the value of the data, and perhaps an auto-increment id to uniquely identify and reference each data item.

 

you would write a query that retrieves the data you want, in the order that you want it. you would then just display the data the way you want when you loop over the row(s) that the query returned.

Link to comment
https://forums.phpfreaks.com/topic/301035-php-mysql-loop/#findComment-1532169
Share on other sites

You should not store data in databases as spreadsheets. You should normalize the data and use the database correctly.
 
So, instead of

table1
+--------+------------+-------+-------+--   --+-------+
| recid  | other data | MON1  | MON2  |  ...  | MON50 |
+--------+------------+-------+-------+--   --+-------+
|  123   | XYZ1234    |  aaa  |  bbb  |  ...  |  ccc  |
+--------+------------+-------+-------+--   --+-------+

you should have

                 table1
+--------+------------+
| recid  | other data |
+--------+------------+
|  123   | XYZ1234    |
+--------+------------+
    |
    +--------------------------------+
                                     |
                                     |                     table2
                          +------+--------+------------+--------+
                          |  id  | recid  | date       | value  |
                          +------+--------+------------+--------+
                          |   1  |  123   | 2016-01-01 |  aaa   |
                          |   2  |  123   | 2016-01-02 |  bbb   |
                          |   3  |  123   |    ...     |  ...   |
                          |   4  |  123   | 2016-01-03 |  ccc   |
                          +------+--------+------------+--------+
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/301035-php-mysql-loop/#findComment-1532172
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.