...

View Full Version : How to improve this clumsy code with a loop



cast_no_shadow
08-20-2012, 12:16 PM
Hello,

I wrote this code that is working but I find it very clumsy.

I want to print the scores of every lesson only if the score is higher than 0. If it is higher than 100 I want to highlight it. However I don't want to write the same


if ($info['lesson1'] == 0){
echo "<p>Lesson 1: ".$info['lesson1'] . " </p>";}

else { if ($info['lesson1'] > 100){
echo "<h1>Lesson 1: ".$info['lesson1'] . " </h1>";} }

for every lesson (I have more than 100 !!!). I'm sure there is a way to do it using a loop, but I don't know how :(

Here is my code:


<?php

if ($_SESSION['username'])
{
echo "<h1> welcome, ".$_SESSION['username']."</h1>";

// The user data

include("connect.php");

$username = $_SESSION['username'];

$data = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());

$info = mysql_fetch_array($data);

if ($_SESSION['username'])
{
Print " <h2>User Information</h2>
<p>Username: ".$info['username'] . "<p>
<hr>
<p>Total points: ".$info['total_points'] . " </p>";


if ($info['lesson1'] == 0){
echo "<p>Lesson 1: ".$info['lesson1'] . " </p>";}

else { if ($info['lesson1'] > 100){
echo "<h1>Lesson 1: ".$info['lesson1'] . " </h1>";} }


if ($info['lesson2'] == 0){
echo "<p>Lesson 2: ".$info['lesson2'] . " </p>";}

else { if ($info['lesson2'] > 100){
echo "<h1>Lesson 2: ".$info['lesson2'] . " </h1>";}}

if ($info['lesson3'] == 0){
echo "<p>Lesson 3: ".$info['lesson3'] . " </p>";}

else {if ($info['lesson3'] > 100){
echo "<h1>Lesson 3: ".$info['lesson3'] . " </h1>";}}

if ($info['lesson4'] == 0){
echo "<p>Lesson 4: ".$info['lesson4'] . " </p>";}

else { if ($info['lesson4'] > 100){
echo "<h1>Lesson 4: ".$info['lesson4'] . " </h1>";} }



}

}
else die("You must log in");



?>

Could you PLEASE help me?

Thanks a lot!

Len Whistler
08-20-2012, 02:07 PM
Use a function.



<?php

function output ($lesson, $score) {
if ($score > 0) {
echo "Lesson: $lesson Score: $score<br>";
}
if ($score > 100) {
echo "<h1>Lesson: $lesson Score: $score</h1>";
}
return;
}


output ('3','74');
output ('3','0');
output ('3','124');
output ('3','79');
output ('4','0');
output ('3','0');
?>


Output

Lesson: 3 Score: 74
Lesson: 3 Score: 124
<h1>Lesson: 3 Score: 124</h1>
Lesson: 3 Score: 79




-----

djm0219
08-20-2012, 02:15 PM
The way you have the code now your explanation of what you want shown is not correct. You said


I want to print the scores of every lesson only if the score is higer than 0.

The way you have it now it will only show something if the lesson value is 0 or greater than 100. Also, please tell us you do not have a single table with a column for every lesson! If you do please look into normalization of your data.

Here's an easy way to accomplish what you are trying to do based on what your stated criteria is.



<?php
$lessonmax = 3; // set this variable to the maximum number of lessons
if ($_SESSION['username']) {
$username = $_SESSION['username'];
echo "<h1>Welcome, $username</h1>";
include("connect.php");
$data = mysql_query("SELECT * FROM users WHERE username='$username'") or die(mysql_error());
$info = mysql_fetch_array($data);
echo "<h2>User Information</h2>
<p>Username: $username<p>
<hr>
<p>Total points: " . $info['total_points'] . " </p>";

for ($lesson = 1;$lesson <= $lessonmax;$lesson++) {
$lessonvalue = $info['lesson' . $lesson];
if ($lessonvalue > 100){
echo "<h1>Lesson " . $lesson . ": " . $lessonvalue . "</h1>";
} elseif ($lessonvalue > 0) {
echo "<p>Lesson " . $lesson . ": " . $lessonvalue . "</p>";
}
}
} else {
die("You must log in");
}
?>

cast_no_shadow
08-20-2012, 03:15 PM
Also, please tell us you do not have a single table with a column for every lesson! If you do please look into normalization of your data.

Thanks a lot for your help.

I'm afraid that yes, I have a column for every lesson.... (I'm new in coding).

I don't understand what you mean by "normalization of your data", Could you please elaborate?

Thanks again.

djm0219
08-20-2012, 03:40 PM
I don't understand what you mean by "normalization of your data", Could you please elaborate?

http://www.tuxradar.com/practicalphp/9/8/0
http://en.wikipedia.org/wiki/Database_normalization
http://support.microsoft.com/kb/283878
http://databases.about.com/od/specificproducts/a/normalization.htm

NOW is the time to do it. It will make your life a LOT easier. It's easier to do than most people realize.

Len Whistler
08-20-2012, 03:54 PM
"normalization of your data" - If your columns hold the same info then you might be better off moving that info to a new table, in your case instead of one table you would require three tables and then JOIN them during the query.



STUDENTS
student_id | student_name
00001 | Ben
00002 | Larry
00003 | Cathy
00004 | Tom

LESSONS
lesson_id | lesson
01 | boating
02 | basket weaving
03 | fire safety
04 | typing


STUDENT_SCORES
student_id | lesson_id | score
00002 | 02 | 56
00002 | 04 | 0
00003 | 01 | 143
00002 | 03 | 43
00001 | 04 | 456
00003 | 04 | 23




-----



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum