2

I need to make a list that prints the months and if they're paid or not.

So first I make an array with month names:

$month_names = array("January","February","March","April","May","June","July","August","September","October","November","December");

Then I have columns for the twelve months: values 1 for paid 0 for non-paid. So I made a variable to know if it's paid or not:

if ($row['month1'] == '1') {
    $row['month1'] = 'Paid';
} else $row['month1'] = 'Non-Paid';

Is there a better way to do that?

The list will have only two columns: "monthly payments" and "status".

And the while or foreach should be something like:

echo '
    <li><a class="months">'$array[$month_names]=>$value'</a></li>
    <li><a class="status">'.$row['month1'].'</a></li>
    ';

I'm not really sure, how can I do this?

Thank YOU!

Burpy Burp
  • 459
  • 3
  • 12
Chazy Chaz
  • 1,781
  • 3
  • 29
  • 48

2 Answers2

2

If I understood you right you have an array like this:

$rows = array(
    array('month' => 1, 'paid' => 0),
    array('month' => 2, 'paid' => 1),
    array('month' => 4, 'paid' => 0),
    array('month' => 5, 'paid' => 1),
    array('month' => 7, 'paid' => 1),
    array('month' => 8, 'paid' => 0),
);

In this case you can do something like this

<ul class="rows">
    <?php foreach ($rows as $row): ?>
        <li><a href="#" class="month"><?php echo $month_names[$row['month']] ?></a></li>
        <li><a href="#" class="status"><?php echo $row['paid'] ? 'Paid' : 'Non-Paid' ?></a></li>
    <?php endforeach ?>
</ul>

That would be ideal, but I noticed that your example array has a month1 key, so if your array has this structure: month[0-9] = paid-status:

$rows = array(
    'month1' => 0,
    'month2' => 1,
    'month4' => 0,
    'month5' => 1,
    'month7' => 1,
    'month8' => 0,
);

Then you should do something like this:

<ul class="rows">
    <?php foreach ($rows as $month => $status): ?>
        <!-- We replace all non-numeric caracters in the key of the element and then sustract one to match the zero base index -->
        <li><a href="#" class="month"><?php echo $month_names[ preg_replace('/[^0-9]/', '', $month) - 1 ] ?></a></li>
        <li><a href="#" class="status"><?php echo $status ? 'Paid' : 'Non-Paid' ?></a></li>
    <?php endforeach ?>
</ul>
Alex Quintero
  • 1,160
  • 10
  • 21
1

You are on the right track keeping your logic separate from the presentation. That is a good thing. For your problem, you can check all of your month values and store the results in another array (let's call it $paid), indexed by the number of the month. Then you use your $monthNames array to map to the corresponding value in $paid. That will set you up to loop through the $monthNames array and create your HTML.

The following will do what you need:

<?php

$monthNames = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December");
$paid = array(); //Placeholder for paid months.
$row = $result->fetch_array();  //Assuming MySQLi API (you are using that or PDO...I hope...)

for($i = 1; $i < 13; $i++) {
    $month = "month" . $i;
    if($row[$month] == 1) {
        $paid[] = "Paid";
    } else {
        $paid[] = "Not Paid";
    }
}

//Now make the HTML list

print("<ul>\n");
foreach($monthNames as $key => $month) {
    print('<li><a class="months">' . $month . '</a></li>' . "\n");
    print('<li><a class="status">' . $paid[$key] .'</a></li>' . "\n");
}
print("</ul>");

?>

The output will be along the lines of (depending on the DB data):

<ul>
<li><a class="months">January</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">February</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">March</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">April</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">May</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">June</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">July</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">August</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">September</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">October</a></li>
<li><a class="status">Paid</a></li>
<li><a class="months">November</a></li>
<li><a class="status">Not Paid</a></li>
<li><a class="months">December</a></li>
<li><a class="status">Paid</a></li>
</ul>

Hope that help!

Crackertastic
  • 4,958
  • 2
  • 30
  • 37
  • Wow! Thanks! ofc it helps! :) Hmmm is there any difference between using printf and echo? I'm actually using echo because it's the one i know, it's better to use printf? – Chazy Chaz Oct 01 '14 at 17:44
  • @ChazyChaz There is nothing wrong with `print` vs. `printf`. If anything, the `printf` function is better when dealing with variables in your strings since you have more control over formatting. For basic strings with a variable or two `print` will work fine. – Crackertastic Oct 01 '14 at 17:50
  • Ok, thanks i'll change the echos for print. Thanks for the tips :) – Chazy Chaz Oct 01 '14 at 18:04
  • @ChazyChaz No problem. For what it is worth, there wasn't anything wrong with using `echo` either, I just prefer to `print` or `printf` my strings :) See this [SO post](http://stackoverflow.com/questions/234241/how-are-echo-and-print-different-in-php) for more on the topic of echo vs. print. – Crackertastic Oct 01 '14 at 18:09
  • Can i freely add more columns with $variables without problems? If the statement have selected the table. – Chazy Chaz Oct 01 '14 at 18:17
  • Sure, but keep in mind my `for` loop will only target columns name `month1`, `month2`, `month3` ... `month12`. After that you will need to access the others directly. – Crackertastic Oct 01 '14 at 18:20
  • Yes, it's a column with a id, i need to print that id next to the status, like this: january | PAID | ID, will the loop print the column data for every month? – Chazy Chaz Oct 01 '14 at 18:23
  • Ok i added the variable inside the for. – Chazy Chaz Oct 01 '14 at 18:35
  • One last thing, i have a css class named list. print("
    "); list is highlighted as if it's some especial or reserved word and i'm getting: Parse error: syntax error, unexpected 'list' (T_LIST) in C:\xampp\htdocs\views\user\friend.php on line 65
    – Chazy Chaz Oct 01 '14 at 19:11
  • No, it is a syntax error. (`list()` is a function though, but that doesn't matter.) When using double quotes around strings you must either escape all other double quotes or use single quotes to surround strings with double quotes in them. So do either `print("
    ");` or `print('
    ');` Keep in mind though that using single quotes to surround strings does not interpolate variables into values.
    – Crackertastic Oct 01 '14 at 19:19