While after While is Empty

Hi

Im am trying to get a chart from Fusion Charts to diplay correctly.

Il try here, since the support on fusion Charts page is lacking…

Im selecting to dates and showing the bodyweight variation between the dates.

I can create the array of dates in the while, but the bodyweight does return anything.

HTML


<form method="post" action="index.php?action=profile&sub=stats_bw&id=<?php echo $USER_PROFILE->getId(); ?>"> 

	<table border="0">
    <tr>
    <td>
    <b><?php echo translate('from'); ?></b>	<select id="day" name="fromday">
            <option value=""><?php echo ucfirst(translate('day')); ?></option>
            <?php
            for($i=1; $i<=31; $i++)
            {
                if($i == $blog['fromday'])
                {
                    echo "<option value=\\"" . $i . "\\" selected=\\"selected\\">" . $i . "</option>\
";
                }
                else
                {
                    echo "<option value=\\"" . $i . "\\">" . $i . "</option>\
";
                }
            }
            ?>
        </select>
        
        <select id="month" name="frommonth">
            <option value=""><?php echo ucfirst(translate('month')); ?></option>
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['frommonth'])
                {
                    echo "<option value=\\"" . ($k+1) . "\\" selected=\\"selected\\">" . ucfirst(translate($v)) . "</option>\
";
                }
                else
                {
                    echo "<option value=\\"" . ($k+1) . "\\">" . ucfirst(translate($v)) . "</option>\
";
                }
            }
            ?>
        </select>
    
        <select id="year" name="fromyear">
            <option value=""><?php echo ucfirst(translate('year')); ?></option>
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['fromyear'])
                {
                    echo "<option value=\\"" . $i . "\\" selected=\\"selected\\">" . $i . "</option>\
";
                }
                else
                {
                    echo "<option value=\\"" . $i . "\\">" . $i . "</option>\
";
                }
            }
            ?>
        </select> 
    </td>
        <td>
        <b><?php echo translate('to'); ?></b>
        <select id="day" name="today">
        <option value=""><?php echo ucfirst(translate('day')); ?></option>
        <?php
        for($i=1; $i<=31; $i++)
        {
            if($i == $blog['today'])
            {
                echo "<option value=\\"" . $i . "\\" selected=\\"selected\\">" . $i . "</option>\
";
            }
            else
            {
                echo "<option value=\\"" . $i . "\\">" . $i . "</option>\
";
            }
        }
        ?>
    	</select>
        
        <select id="month" name="tomonth">
            <option value=""><?php echo ucfirst(translate('month')); ?></option>
            <?php
            foreach($MONTHS as $k => $v)
            {
                if(($k+1) == $blog['tomonth'])
                {
                    echo "<option value=\\"" . ($k+1) . "\\" selected=\\"selected\\">" . ucfirst(translate($v)) . "</option>\
";
                }
                else
                {
                    echo "<option value=\\"" . ($k+1) . "\\">" . ucfirst(translate($v)) . "</option>\
";
                }
            }
            ?>
        </select>
        
        <select id="year" name="toyear">
            <option value=""><?php echo ucfirst(translate('year')); ?></option>
            <?php
            for($i=date('Y', $time_now); $i>=(date('Y', $time_now) - 11); $i--)
            {
                if($i == $blog['toyear'])
                {
                    echo "<option value=\\"" . $i . "\\" selected=\\"selected\\">" . $i . "</option>\
";
                }
                else
                {
                    echo "<option value=\\"" . $i . "\\">" . $i . "</option>\
";
                }
            }
            ?>
        </select>     
        <td>
        <input type="hidden" name="user_id" value="<?php echo $USER_PROFILE->getId(); ?>"  />
        <input type="submit" style="font-weight:bold;" name="select" value=" <?php echo ucf(translate('show')); ?> "  />
        </td>
    </table>
</form>

Here is the code after the submit…


if(isset($_POST['select']))
{
	
	$blog['fromday']=$_POST['fromday'];
	$blog['frommonth']=$_POST['frommonth'];
	$blog['fromyear']=$_POST['fromyear'];
	$fromdate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['frommonth'], $blog['fromday'], $blog['fromyear']);
	
	$blog['today']=$_POST['today'];
	$blog['tomonth']=$_POST['tomonth'];
	$blog['toyear']=$_POST['toyear'];
	$todate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['tomonth'], $blog['today'], $blog['toyear']);

	$user_id = $_POST['user_id'];

echo "<br />";
	
	// Get all the data
	$result = mysql_query("SELECT 
	training.date, 
	training.training_id,
	training.place,
	training.bodyweight
	FROM lifts, training
	WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
	ORDER BY training.date") or die(mysql_error());

$num = mysql_num_rows($result);
$i=1;
$strXML = "<chart caption='" . ucf(translate('bodyweight')) . "' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('bodyweight')) . "'>";
$row = mysql_fetch_array($result);

if($i <= $num)
{ 

		$strXML .= "<categories>";
		while($row = mysql_fetch_array($result))
		{
			if($w_metric == "kg")
				{
				 $strXML .= "<category label='" . formatDate($row['date'], $dateformat) . "' />";
				}
				else
				{
				$strXML .= "<category label='" . formatDate($row['date'], $dateformat) . "' />";
				}
		}
		$strXML .= "</categories>";
		
		$strXML .= "<dataset seriesName='Bw'>";
		while($row = mysql_fetch_array($result)) // Is empty :(
		{
			if($w_metric == "kg")
				{
				 $strXML .= "<set value='" . $row['bodyweight'] . "' />";
				 echo "teste";
				}
				else
				{
				$strXML .= "<set value='" . r_nr(calcFromKg($row['bodyweight'])) . "' />";
				}
		}
		$strXML .= "</dataset></chart>"; //</dataset> doesnt show, strange..
			
		
		echo renderChart("Charts/ZoomLine.swf", "", $strXML, "".ucf(translate('bodyweight'))."", 900, 400, 1, 1);
		echo "<div class='fixed'>
							</div>
						</div>
						</div>
					<div class='fixed'>
					</div>
					<div id='footer'>
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
						</div>";
			exit;
			
}

XML debug, as you can see the <dataset> tag return empty


<chart caption="Kroppsvekt" subCaption="For Stian Walgermo" showBorder="1" formatNumberScale="0" numberSuffix="kg" baseFont="Arial" exportEnabled="1" exportAtClient="0" exportHandler="Charts/ExportHandlers/PHP/FCExporter.php" exportAction="download" exportTargetWindow="_blank" exportFileName="Kroppsvekt"><categories><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /><category label="21.02.2011" /></categories><dataset seriesName="Bw" /></chart>

Your second while is returning zero because you have already passed through the array in with the first while statement.

You can only parse through the result array once

Is there some other ways to do this? :slight_smile:

Use [fphp]mysql_data_seek[/fphp] to reset the pointer on the result to the first row.


while( $row=mysql_fetch_assoc($result) )
{
   // ...
}
mysql_data_seek($result, 0);
while( $row=mysql_fetch_assoc($result) )
{
   // ...
}

:slight_smile:

OR

just tidy your code.


if(isset($_POST['select'])) 
{ 
 
    $blog['fromday']=$_POST['fromday']; 
    $blog['frommonth']=$_POST['frommonth']; 
    $blog['fromyear']=$_POST['fromyear']; 
    $fromdate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['frommonth'], $blog['fromday'], $blog['fromyear']); 
 
    $blog['today']=$_POST['today']; 
    $blog['tomonth']=$_POST['tomonth']; 
    $blog['toyear']=$_POST['toyear']; 
    $todate = mktime(date('H', $time_now), date('i', $time_now), date('s', $time_now), $blog['tomonth'], $blog['today'], $blog['toyear']); 
 
    $user_id = $_POST['user_id']; 
 
echo "<br />"; 
 
    // Get all the data 
    $result = mysql_query("SELECT  
    training.date,  
    training.training_id, 
    training.place, 
    training.bodyweight 
    FROM lifts, training 
    WHERE training.training_id=lifts.training_id AND training.user_id =". $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . " 
    ORDER BY training.date") or die(mysql_error()); 
 
$strXML = "<chart caption='" . ucf(translate('bodyweight')) . "' subCaption='" . ucf(translate('for')) . " " . getNameFromId($USER_PROFILE->getId()) . "' showBorder='1' formatNumberScale='0' numberSuffix='". $w_metric ."' baseFont='Arial' exportEnabled='1' exportAtClient='0' exportHandler='Charts/ExportHandlers/PHP/FCExporter.php' exportAction='download' exportTargetWindow='_blank' exportFileName='" . ucf(translate('bodyweight')) . "'>"; 
 
if(mysql_num_rows($result) > 0) 
{  
        $catstrXML = "<categories>";  // make category substring
        $datastrXML = "<dataset seriesName='Bw'>"; // make dataset substring
 
        while($row = mysql_fetch_array($result)){  // loop through data
               $catstrXML .= "<category label='" . formatDate($row['date'], $dateformat) . "' />";   // category always same so no need to be within if KG statements
               if($w_metric == "kg"){ 
                   $datastrXML .= "<set value='" . $row['bodyweight'] . "' />"; 
               }else{ 
                   $datastrXML .= "<set value='" . r_nr(calcFromKg($row['bodyweight'])) . "' />"; 
               } // end if
        } // end while
        $catstrXML .= "</categories>"; // finish substring
        $datastrXML .= "</dataset>"; // finish substring
        $strXML .= $catstrXML . $datastrXML . "</chart>";  // add substrings to main string
 
 
        echo renderChart("Charts/ZoomLine.swf", "", $strXML, "".ucf(translate('bodyweight'))."", 900, 400, 1, 1); 
        echo "<div class='fixed'> 
                            </div> 
                        </div> 
                        </div> 
                    <div class='fixed'> 
                    </div> 
                    <div id='footer'> 
                    <a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a> 
                        <div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br /> 
                        </div>"; 
            exit; 
 
}  

Yes, thanks :slight_smile: Works perfect now :wink:

It seems like it creates dublicate values for date and bodyweight…

Show me whats in your database table and a sample XML output for that data

Confirm that you using my code above if you have modified it post the current version

I figured it out :slight_smile:

Had to add DISTINCT to this query

// Get all the data
$result = mysql_query(“SELECT DISTINCT <– Here
training.date,
training.training_id,
training.place,
training.bodyweight
FROM lifts, training
WHERE training.training_id=lifts.training_id AND training.user_id =”. $user_id ." AND training.date > " . $fromdate . " AND training.date < " . $todate . "
ORDER BY training.date") or die(mysql_error());

OK, but I wouldnt have thought youd have need that, perhaps youve a problem with your database layout ?