Jump to content

while / if else problem


ruraldev

Recommended Posts

I am trying to use the following code snippet to return values from a database with a "," after each value apart form the last one. At the moment for testing I have 2 rows with values 1 and 3

 

$totalRows_Recordset3    equals 2 (I echoed this to check)

 

 

$i=1;  while($row_Recordset3 = mysql_fetch_assoc($Recordset3))  {    if($i < $totalRows_Recordset3)    {      $str = $str.$row_Recordset3['reading'].",";      $i=$i+1;    }    else    {      $str = $str.$row_Recordset3['reading'];      $i=$i+1;    }  } 

 

 

I get the number 3 which is the last value in the database, but I can't see why I don't get "1,3"

 

I am sure it is something simple but it just escapes me.

 

Thanks in advance for any help

 

Gordon

Link to comment
Share on other sites

I just get the 3 and the new row value 10

 

However I noticed I an also getting

 

Warning: implode() [function.implode]: Invalid arguments passed in /home/ruraldev/public_html/wind/solarpower.php on line 81

 

If it helps here is the query code

$query_Recordset3 = "SELECT readingID, reading FROM solar ORDER BY readingID";
$Recordset3 = mysql_query($query_Recordset3, $wind) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

 

The table is

 

readingID date             reading

1         2010-10-04 1

2         2010-10-05 3

3         2010-10-06 10

Link to comment
Share on other sites

ok...

1. make your db connection then...

 

$query_Recordset3 = "SELECT * FROM solar ORDER BY readingID";
$Recordset3 = mysql_query($query_Recordset3);
$i=0;
while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
$new_array[$i] = $row_Recordset3['reading'];
$i ++;
}
$count=0;
$count = count($new_array);
if($count>1) {
$string = implode(",", $new_array);
}else{
echo $count;
}

Link to comment
Share on other sites

This is the whole page    http://www.ruraldevelopmenttrust.co.uk/wind/solarpower.php

There are some notes from the template I used which I haven't deleted yet

 

<?php require_once('../Connections/wind.php'); ?>
<?php
mysql_select_db($database_wind, $wind);
$query_Recordset1 = "SELECT readingID, date_format(date,'%d/%m/%Y') as date, reading FROM solar ORDER BY readingID DESC LIMIT 5";
$Recordset1 = mysql_query($query_Recordset1, $wind) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_wind, $wind);
$query_Recordset2 = "SELECT datediff( Max(date), Min(date)) as DateDiff,  Max(reading) as LatestReading, date_format(MAX(date),'%d/%m/%Y') as LatestDate FROM solar";
$Recordset2 = mysql_query($query_Recordset2, $wind) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);

mysql_select_db($database_wind, $wind);
$query_Recordset3 = "SELECT * FROM solar ORDER BY readingID";
$Recordset3 = mysql_query($query_Recordset3);


  

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Solar PV</title>
<style type="text/css">
<!--
body {
    background-color: #66CCCC;
}
-->
</style></head>

<body><table width="800" border="0" align="center">
  <tr>
    <td><p class="style1">The panels were installed on 20/10/2010 by <?php echo $row_Recordset2['LatestDate']; ?> </p>
<p class="style1">the panels had generated <?php echo $row_Recordset2['LatestReading']; ?> units of electricity</p>
<p class="style1">that works out at an average of <?php echo (int)(($row_Recordset2['LatestReading'])/($row_Recordset2['DateDiff'])); ?> units per day. </p>
<table border="1">
  <tr>
    <td class="style1">Reading ID</td>
    <td class="style1">Reading Date</td>
    <td class="style1">Units Generated </td>
  </tr>
  <?php do { ?>
    <tr>
      <td class="style1"><div align="center"><?php echo $row_Recordset1['readingID']; ?></div></td>
      <td class="style1"><?php echo $row_Recordset1['date']; ?></td>
      <td class="style1"><div align="center"><?php echo $row_Recordset1['reading']; ?></div></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p class="style1"> </p></td>
  </tr>
</table>
<?php

while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
  echo $row_Recordset3['reading'] . "<br>";
}
$str = "";
  $str .=  "<img src='http://chart.apis.google.com/chart?chs=240x100";
  $str .= "&chd=t:"; 


$i=0;
while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
$new_array[$i] = $row_Recordset3['reading'];
$i ++;
}
$count=0;
$count = count($new_array);
if($count>1) {
$string = implode(",", $new_array);
}else{
echo $count;
}
echo $string;
$str = $str.$string;

  $str = $str."&cht=lc";
  // We will close the src attribute with \' and to print escape character ' we shall precede it with \

  $str = $str."&chxt=x,y'";
  $str = $str." >";

  //we shall echo the $str that will display the graph
  echo $str;

?> 
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-374595-5";
urchinTracker();
</script>
</body>
</html>
<?php
mysql_free_result($Recordset1);
?>

Link to comment
Share on other sites

<?php

$i=0;
while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
$new_array[$i] = $row_Recordset3['reading'];
$i ++;
}
$count=0;
$count = count($new_array);
if($count>1) {
$string = implode(",", $new_array);
}else{
echo $count;
}
echo $string;

?> 

 

Could just as easily be:

<?php
$i=0;
while($row_Recordset3 = mysql_fetch_assoc($Recordset3)){
    echo $i++ === 0 ? $row_Recordset3[ 'reading' ] : ', ' . $row_Recordset3[ 'reading' ];
}
?>

Link to comment
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.