Problem using Count() in a while loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Problem using Count() in a while loop

Post by mmc01ms »

im trying to get one of the coloumns in my table to output the number of presentations related to a meeting using the COUNT(). It works the only problem is that i want it to count for every meeting entry in the database. Because i use a while loop the loop continues and doesn't end where before i put in this feature count() it outputed the data in the database and then finished. Any ideas?

Code: Select all

<?php

require_once('../../../classes/database.class.php');

$database = new database();
$link_id = $database->database_connection();
$query = "select * from epc_meetings order by meetingid desc";
$result = mysql_query($query) or die(mysql_error());
$total_rows = mysql_num_rows($result);
$file = mysql_fetch_array($result);
if (!$result)    
    {
        print 'There was a database error when executing';
        print mysql_error();
        exit;
    }
    

    
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>EPC Website Administration: Meetings</title>
<link href="../../css/epc_admin.css" rel="stylesheet" type="text/css" />
<script language="JavaScript" type="text/JavaScript">
<!--
function BRB_PHP_DelWithCon(deletepage_url,field_name,field_value,messagetext) { //v1.0 - Deletes a record with confirmation
  if (confirm(messagetext)==1){
      location.href = eval('\"'+deletepage_url+'?'+field_name+'='+field_value+'\"');
  }
}
//-->
</script>
<script src="../../includes/sorttable.js"></script>
</head>

<body>
<div id="heading">
<h1><a href="../index.php">Website Adminstration</a>: Meetings</h1>
<img src="../../images/!genric/epc2.jpg" width="305" height="54" />
<div class="clear"></div>
</div>

<div id="main">
<p>[<a href="upload.php">Add a new meeting</a>]</p>
<?php if($total_rows == 0)
    { ?>
        <p>There are currently no meeting entries in the database</p>
    <?
    }
    if ($total_rows > 0) { ?>
    <table border="0" cellpadding="0" cellspacing="0" class="indexTable sortable" id="indexTable">
        <tr>
            <th scope="col">Event</th>
            <th scope="col">Location</th>
            <th scope="col">Year</th>
            <th scope="col">Presentations</th>
            <th colspan="3">Action</th>        
        </tr>
        <?php do { ?>
        <tr>
            <td><?php print  $file['event']; ?></td>
            <td><?php print  $file['location']; ?></td>
            <td><?php print $file['year']; ?></td>
            <td>
            <?php
            $id = $file['meetingid'];
            $database = new database();
            $link_id = $database->database_connection();
            $query = "select * from epc_meeting_presentations where meetingid = '$id'";
            $result = mysql_query($query) or die(mysql_error());
            if (!$result)    
            {
            print 'There was a database error when executing';
            print mysql_error();
            exit;
            }
            echo count($result);
            
            ?>
            </td>
            <td class="narrow"><?php print '<a href="edit_file.php?id='.$file['meetingid'].'">Edit</a>'; ?></td>
            <td class="narrow"><a href="javascript:BRB_PHP_DelWithCon('delete.php', 'meetingid',<?php echo $file['meetingid'];?>, 'Are you sure you want to delete this entry?');">Delete</a></td>
            <td class="narrow"><?php print '<a href="presentations/index.php?id='.$file['meetingid'].'">View Presentations</a>'; ?></td>
        </tr>
        <?php } while ($file = mysql_fetch_array($result)); ?>
  </table>
    <?php } ?>
    <p>&nbsp;</p>
</div>
</body>
</html>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're counting a result resource from a query.. you can't do that. :?
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

so how do i get the $result converted into a foramt the COUNT can use?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Post Reply