Page 1 of 2

Get only last value of a dynamic array

Posted: Mon Jun 28, 2004 7:07 am
by Canadian Pickle
Hi All,

I am trying to only show the last and final value of each array.

The problem is, each array is not a set number of values, and therefore I have no way to get the last one in each set.

Any help would be great.

Here is the code I have:

Code: Select all

<?php
mysql_select_db($database_waxyourboard, $waxyourboard);
$query_rsgetinfo = "SELECT FirstName, LastName, GolferID, Enter_Score.GolfDate, (Hole1 + Hole2 + Hole3 + Hole4 + Hole5 + Hole6 + Hole7 + Hole8 + Hole9) as Grandtotal, magicnum FROM Enter_Score, Golfer, MagicNumber WHERE Golfer.GolferID = Enter_Score.Golfer AND MagicNumber.golfdate = Enter_Score.GolfDate";
$rsgetinfo = mysql_query($query_rsgetinfo, $waxyourboard) or die(mysql_error());
$row_rsgetinfo = mysql_fetch_assoc($rsgetinfo);
$totalRows_rsgetinfo = mysql_num_rows($rsgetinfo);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="1" align="center">
  <?php do {
  
  $a = $row_rsgetinfo['Grandtotal'];
  $b = $row_rsgetinfo['magicnum'];
  $c = $row_rsgetinfo['GolferID'];
  
  foreach($name as $test1 => $total)
  {
  $e = $total;
  }
  
  switch($a - $b)
  {
  // Look for people who got 10 points
  case "0":
  case "8":
  case "16":
  case "24":
  case "-8":
  case "-16":
  case "-24":
  $name[$c] += "10";
  break;
  // Look for people who got 9 points
  case "1":
  case "7":
  case "9":
  case "15":
  case "17":
  case "23":
  case "25":
  case "-1":
  case "-7":
  case "-9":
  case "-15":
  case "-17":
  case "-23":
  case "-25":
  $name[$c] += "9";
  break;
  // Look for people who got 8 points
  case "2":
  case "6":
  case "10":
  case "14":
  case "18":
  case "22":
  case "-2":
  case "-6":
  case "-10":
  case "-14":
  case "-18":
  case "-22":
  $name[$c] += "8";
  break;
  // Look for people who got 7 points
  case "3":
  case "5":
  case "11":
  case "13":
  case "19":
  case "21":
  case "-3":
  case "-5":
  case "-11":
  case "-13":
  case "-19":
  case "-21":
  $name[$c] += "7";
  break;
  // Look for people who got 6 points
  case "4":
  case "12":
  case "20":
  case "-4":
  case "-12":
  case "-20":
  $name[$c] += "6";
  break;
  default:
  $name[$c] += "0";
  break;
  }
  ?>
  <tr>
    <td width="78"><div align="center">Tot Score</div></td>
    <td width="78"><div align="center">Magic Num</div></td>
    <td width="78"><div align="center">Name of Golfer</div>
    </td>
    <td width="73"><div align="center">Points Given</div>
    </td>
  </tr>
  <tr>
    <td><div align="center"><?php echo $row_rsgetinfo['Grandtotal']; ?></div></td>
    <td><div align="center"><?php echo $row_rsgetinfo['magicnum']; ?></div>
    </td>
    <td><div align="center"><?php echo $row_rsgetinfo['FirstName']; ?> <?php echo $row_rsgetinfo['LastName']; ?> </div>
    </td>
    <td><center>
        <?php echo $e; ?>
        </center>
    </td>
  </tr>
  <?php } while ($row_rsgetinfo = mysql_fetch_assoc($rsgetinfo)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($rsgetinfo);
?>

Posted: Mon Jun 28, 2004 7:13 am
by patrikG
[php_man]array_pop[/php_man] should help you there. If you want to leave the original array intact, use [php_man]array_values[/php_man], e.g.

Code: Select all

$new_array  =  array_pop(array_values($old_array));
Obviously, keys are not preserved.

Posted: Mon Jun 28, 2004 7:13 am
by Grim...
If you don't care about keeping the array intact then [php_man]array_pop[/php_man]() will work for you.
Otherwise, use [php_man]array_reverse[/php_man](), and use the first result

Code: Select all

$array&#1111;0]

Posted: Mon Jun 28, 2004 7:15 am
by Grim...
Or count them:

Code: Select all

<?php

$array = array (1, 5, 3, 10, 11, 0, 4);

$num = count($array) - 1;

echo '$array[$num]';
?>

Posted: Mon Jun 28, 2004 7:19 am
by redmonkey

Posted: Mon Jun 28, 2004 7:19 am
by patrikG
Grim... wrote:Or count them:

Code: Select all

<?php

$array = array (1, 5, 3, 10, 11, 0, 4);

$num = count($array) - 1;

echo '$array[$num]';
?>
This works, but why let PHP do the extra work when there are in-built functions for it?

Posted: Mon Jun 28, 2004 7:31 am
by Canadian Pickle
Hello,

Thanks Grim and PatrickG but none of those worked!

The problem is I add values to the array dynamically, and then need to display only the final count.

ie. For say user Bob I need to show he had a total or 45, where now it shows :

Bob 8, Bob 19, Bob 26, Bob 35, Bob 45.

I would like to only show Bob 45.

thanks

Posted: Mon Jun 28, 2004 7:34 am
by patrikG
Canadian Pickle wrote:Thanks Grim and PatrickG but none of those worked!
what does array_pop($my_array) return, then?

btw: patrikG (sans "c") ;)

Posted: Mon Jun 28, 2004 7:38 am
by Canadian Pickle
Sorry patrikG,

It returns all the values with it totaling:

For example:

Bob 9
Bob 20
Bob 31
Bob 45
Dave 8
Dave 17
etc.

Posted: Mon Jun 28, 2004 7:40 am
by patrikG
post the code you're using, please.

Posted: Mon Jun 28, 2004 7:42 am
by Canadian Pickle
Ok here is the exact code I am using to do this in Dreamweaver:

Code: Select all

<?php require_once('Connections/waxyourboard.php'); ?>
<?php
mysql_select_db($database_waxyourboard, $waxyourboard);
$query_rsgetinfo = "SELECT FirstName, LastName, GolferID, Enter_Score.GolfDate, (Hole1 + Hole2 + Hole3 + Hole4 + Hole5 + Hole6 + Hole7 + Hole8 + Hole9) as Grandtotal, magicnum FROM Enter_Score, Golfer, MagicNumber WHERE Golfer.GolferID = Enter_Score.Golfer AND MagicNumber.golfdate = Enter_Score.GolfDate";
$rsgetinfo = mysql_query($query_rsgetinfo, $waxyourboard) or die(mysql_error());
$row_rsgetinfo = mysql_fetch_assoc($rsgetinfo);
$totalRows_rsgetinfo = mysql_num_rows($rsgetinfo);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table border="1" align="center">
  <?php do {
  
  $a = $row_rsgetinfo['Grandtotal'];
  $b = $row_rsgetinfo['magicnum'];
  $c = $row_rsgetinfo['GolferID'];
  
 // foreach($name as $test1 => $total)
  //{
 // $e = $total;
 // }
 
  switch($a - $b)
  {
  // Look for people who got 10 points
  case "0":
  case "8":
  case "16":
  case "24":
  case "-8":
  case "-16":
  case "-24":
  $name[$c] += "10";
  break;
  // Look for people who got 9 points
  case "1":
  case "7":
  case "9":
  case "15":
  case "17":
  case "23":
  case "25":
  case "-1":
  case "-7":
  case "-9":
  case "-15":
  case "-17":
  case "-23":
  case "-25":
  $name[$c] += "9";
  break;
  // Look for people who got 8 points
  case "2":
  case "6":
  case "10":
  case "14":
  case "18":
  case "22":
  case "-2":
  case "-6":
  case "-10":
  case "-14":
  case "-18":
  case "-22":
  $name[$c] += "8";
  break;
  // Look for people who got 7 points
  case "3":
  case "5":
  case "11":
  case "13":
  case "19":
  case "21":
  case "-3":
  case "-5":
  case "-11":
  case "-13":
  case "-19":
  case "-21":
  $name[$c] += "7";
  break;
  // Look for people who got 6 points
  case "4":
  case "12":
  case "20":
  case "-4":
  case "-12":
  case "-20":
  $name[$c] += "6";
  break;
  default:
  $name[$c] += "0";
  break;
  }
  ?>
  <tr>
    <td width="78"><div align="center">Tot Score</div></td>
    <td width="78"><div align="center">Magic Num</div></td>
    <td width="78"><div align="center">Name of Golfer</div>
    </td>
    <td width="73"><div align="center">Points Given</div>
    </td>
  </tr>
  <tr>
    <td><div align="center"><?php echo $row_rsgetinfo['Grandtotal']; ?></div></td>
    <td><div align="center"><?php echo $row_rsgetinfo['magicnum']; ?></div>
    </td>
    <td><div align="center"><?php echo $row_rsgetinfo['FirstName']; ?> <?php echo $row_rsgetinfo['LastName']; ?> </div>
    </td>
    <td><center>
        <?php echo array_pop(array_values($name));?>
        </center>
    </td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <?php } while ($row_rsgetinfo = mysql_fetch_assoc($rsgetinfo)); ?>
</table>
</body>
</html>
<?php
mysql_free_result($rsgetinfo);
?>

Posted: Mon Jun 28, 2004 7:52 am
by Grim...
patrikG wrote: btw: patrikG (sans "c") ;)
btw: Grim... (with dots) ;)

Posted: Mon Jun 28, 2004 7:54 am
by patrikG
it's not working because you're populating the array (see: your while-statement) while trying to determine the last element of you array (array_pop).
That's comparable to seeing the first carriage of a train coming out of a tunnel and asking "How long is the train? And what colour does the last carriage have". Answer: you don't and you can't know. Let it come out of the tunnel first. Then count.

Break down what you're trying to do into logical unit and treat every unit as seperate entity:

1. Obtain data from the database
2. Filter/Manipulate the data (e.g. determine last element of an array)
3. Display the data (in your HTML-selects).

You will find it makes things much easier

Posted: Mon Jun 28, 2004 8:11 am
by Canadian Pickle
Ok.

I see what you are saying, but I am fairly new to PHP and have no idea where to start this.

Any suggestions?

Posted: Mon Jun 28, 2004 9:26 am
by patrikG
Very much as I've described it above.

First: PHP only: retrieve all the data from the database.
Second: PHP only: prepare the data for presentation (in your case: fetch the respective last elements of the arrays you've retrieve in the first step).
Third: HTML (interspersed with echoing of PHP-variables): create the HTML-page, including form-fields, combo-boxes etc.

Break up the webpage you've created. Put all the PHP that contains logic at the top of your page (for an explanation see: http://wiki.devnetwork.net/index.php/TemplatesTutorial and scroll down to "What is content what is logic?"). Once you've done that, close your php-tags and write your HTML.

Once you've done that and got it working, you may want to look into functions.