Jump to content

Recommended Posts

Hey all,

i have a problem with some code, what im trying to create is a color coded percentage.

so i fill in a field in a database with a number as a %'age of what i have done.

if its under 25% then the color is red under 50 it is orange under 75 it is yellow and 100 is green for file finished.

 

Now, in the code i have it pull the info out from the database and show it in a list so visitors can see how the files are coming along.

now everything was fine untill i added the code for the colors and if they were under or over a certain %'age, now it repeats the info in the database 8 times and the very last set has the right colors.

 

so it repeats everythin 7 times and on the 8th time it has the right colors for the right %'age.

 

Can anyone help me fix this code so that it will only show it once and not 8 times.

 

Thanks

Rich.

Here is the code: P.S i have already tried limit 1.

<link rel="stylesheet" type="text/css" href="stylenews.css">
<?
include('data.php');
//include("funktioner.php");
mysql_connect($server,$anvandare, $losen);
mysql_select_db($databas);
$query = mysql_query("SELECT * from todo Order by percentage");
while ($r = mysql_fetch_array($query)) {
?>
<?

if ($r[$percentage] <= 25) {
$fontcol=red;
}
if ($r[$percentage] >= 25) {
$fontcol=orange;
}
if ($r[$percentage] >= 50) {
$fontcol=yellow;
}
if ($r[$percentage] >=75) {
$fontcol=green;
}
if ($r[$percentage] >=100) {
$fontcol=green;
} ?>
<center>
<?=$note?></center>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D6765" width="95%" id="AutoNumber1">
    <tr>
      <td width="100%"><br>
<?
$results= mysql_query("SELECT * FROM todo Order by percentage desc");
$id = "id";
$name = "name";
$description = "description";
$left_to_do = "left to do";
$percentage = "percentage";

echo mysql_error();

if (mysql_Numrows($results)>0)                            //if there are records in the fields
{
  $numrows=mysql_NumRows($results);                       //count them
  $x=0;
  while ($x<$numrows){   //loop through the records

    $theid=mysql_result($results,$x,$id);
    $thename=mysql_result($results,$x,$name);
    $thedescription=mysql_result($results,$x,$description);
    $theltd=mysql_result($results,$x,$left_to_do);
    $thepercentage=mysql_result($results,$x,$percentage);
    
?>
<table width="100%">
    <tr>
        <th class="subcat" width="60%">
            <font size="3"><?=$thename?>:</font>

        </th>
        <th class="subcat">
            <font size="2">Done so far: <font color="<?=$fontcol?>"><?=$thepercentage?>%</font>
        </th>
    </tr>
    <tr>
        <td colspan="2" class="row2">
        Design Page.<BR>

            <?=$theltd?></td>
    </tr>
</table>
<BR>
<?
    $x++;
  }
} else {
?>
<center><b>There is nothing left to do !</b></center>
<br>
<?
}
?>
      </td>
    </tr>
  </table>
  </center>
</div>
<?}?>

Link to comment
https://forums.phpfreaks.com/topic/68691-solved-problem-need-help-with-this-code/
Share on other sites

after a 10 second look

shouldn't

$r[$percentage]

 

be

$r['percentage']

 

 

EDIT: Ok not sure of the problem..

if you only wanted the first record then why are you looping ?

  while ($x<$numrows){  //loop through the records

 

 

Well what it is, is theres 8 rows in the database and i need to show all of them, thats what the loop is for, so that it shows all the info in the database (its a bit like a news section where you enter it and want to show all the news posted).

 

But it repreats the information 8 times instead of just once. i will upload it to my server and send the link so you can see exactly what i mean.

 

Thanks

Rich

EDIT: here is the link http://lilysretreat.richsprivatesite.com/lobby.php

OK basically your looping twice

 

$query = mysql_query("SELECT * from todo Order by percentage");
while ($r = mysql_fetch_array($query)) {

 

then

$results= mysql_query("SELECT * FROM todo Order by percentage desc");

 

try this (untested and was quick review)

<link rel="stylesheet" type="text/css" href="stylenews.css">
<?
include('data.php');
//include("funktioner.php");
mysql_connect($server,$anvandare, $losen);
mysql_select_db($databas); ?>

<center><?=$note?></center>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D6765" width="95%" id="AutoNumber1">
	<tr>
	  <td width="100%"><br>
<?
$results= mysql_query("SELECT * FROM todo Order by percentage desc");
$id = "id";
$name = "name";
$description = "description";
$left_to_do = "left to do";
$percentage = "percentage";

if ($r[$percentage] <= 25) {
$fontcol=red;
}
if ($r[$percentage] >= 25) {
$fontcol=orange;
}
if ($r[$percentage] >= 50) {
$fontcol=yellow;
}
if ($r[$percentage] >=75) {
$fontcol=green;
}
if ($r[$percentage] >=100) {
$fontcol=green;
}

echo mysql_error();

if (mysql_Numrows($results)>0)                            //if there are records in the fields
{
  $numrows=mysql_NumRows($results);                       //count them
  $x=0;
  while ($x<$numrows){   //loop through the records

	$theid=mysql_result($results,$x,$id);
	$thename=mysql_result($results,$x,$name);
	$thedescription=mysql_result($results,$x,$description);
	$theltd=mysql_result($results,$x,$left_to_do);
	$thepercentage=mysql_result($results,$x,$percentage);

?>
<table width="100%">
	<tr>
		<th class="subcat" width="60%">
			<font size="3"><?=$thename?>:</font>

		</th>
		<th class="subcat">
			<font size="2">Done so far: <font color="<?=$fontcol?>"><?=$thepercentage?>%</font>
		</th>
	</tr>
	<tr>
		<td colspan="2" class="row2">
		Design Page.<BR>

			<?=$theltd?></td>
	</tr>
</table>
<BR>
<?
	$x++;
  }
} else {
?>
<center><b>There is nothing left to do !</b></center>
<br>
<?
}
?>
	  </td>
	</tr>
  </table>
  </center>
</div>
<?php
}
?>

Thanks,

When changing it you had missed abit out.

 

while ($r = mysql_fetch_array($query)) {

I added that under the

$results= mysql_query("SELECT * FROM todo Order by percentage desc");

and it works fine.

 

Thanks soo much for your help.

Rich

Yeah, but i posted it like thatbecause i saw others that had solved on it, so i thought i would do that so people knew when it was solved.

 

but then i noticed at the bottom of the page there is a button that says "solved" so i guess you click it and it marks it as bein solved lol.

if that maks sense.

 

Rich

It was, but i just noticed another problem.

 

Now when one is a certain percent, all the colors change to the specified color instead of just that one.

 

I.E If i do 100% on the kitchen and then go and change todo the study and do 30% on that, then all the %'ages goto green, instead of just the kitchen.

so kitchen should be green as its on 100% but the study should be orange as it is only 30%

 

Any ideas?

yep

 

move

	if ($r[$percentage] <= 25) {
$fontcol=red;
}
if ($r[$percentage] >= 25) {
$fontcol=orange;
}
if ($r[$percentage] >= 50) {
$fontcol=yellow;
}
if ($r[$percentage] >=75) {
$fontcol=green;
}
if ($r[$percentage] >=100) {
$fontcol=green;
}

 

below the

 

$thepercentage=mysql_result($results,$x,$percentage);

 

 

 

Full code

<link rel="stylesheet" type="text/css" href="stylenews.css">
<?
include('data.php');
//include("funktioner.php");
mysql_connect($server,$anvandare, $losen);
mysql_select_db($databas); ?>

<center><?=$note?></center>
<div align="center">
  <center>
  <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#5D6765" width="95%" id="AutoNumber1">
	<tr>
	  <td width="100%"><br>
<?
$results= mysql_query("SELECT * FROM todo Order by percentage desc");
$id = "id";
$name = "name";
$description = "description";
$left_to_do = "left to do";
$percentage = "percentage";

echo mysql_error();

if (mysql_Numrows($results)>0)                            //if there are records in the fields
{
  $numrows=mysql_NumRows($results);                       //count them
  $x=0;
  while ($x<$numrows){   //loop through the records

	$theid=mysql_result($results,$x,$id);
	$thename=mysql_result($results,$x,$name);
	$thedescription=mysql_result($results,$x,$description);
	$theltd=mysql_result($results,$x,$left_to_do);
	$thepercentage=mysql_result($results,$x,$percentage);

if ($thepercentage <= 25) {
$fontcol="red";
}
if ($thepercentage >= 25) {
$fontcol="orange";
}
if ($thepercentage >= 50) {
$fontcol="yellow";
}
if ($thepercentage >=75) {
$fontcol="green";
}
if ($thepercentage >=100) {
$fontcol=green;
}

?>
<table width="100%">
	<tr>
		<th class="subcat" width="60%">
			<font size="3"><?=$thename?>:</font>

		</th>
		<th class="subcat">
			<font size="2">Done so far: <font color="<?=$fontcol?>"><?=$thepercentage?>%</font>
		</th>
	</tr>
	<tr>
		<td colspan="2" class="row2">
		Design Page.<BR>

			<?=$theltd?></td>
	</tr>
</table>
<BR>
<?
	$x++;
  }
} else {
?>
<center><b>There is nothing left to do !</b></center>
<br>
<?
}
?>
	  </td>
	</tr>
  </table>
  </center>
</div>
<?php
}
?>

 

 

EDIT: UPDATED Full Code

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.