Jump to content

[SOLVED] problem with mysql_fetch_assoc is there a better way? or error?


simon551

Recommended Posts

My code works. But then it doesn't work. I am printing out details for a line item in a repeat region if there are multiple distinct details to print. (this is for keeping track of travel. sometimes our people travel for more than one project so I print those details, but if there is only one project I just want to show the project once even if there are multiple travel itinerary lines.)

 

In my code, if there are multiple projects -- if ($totalRows_rsProjCount !=1

then the details print.

 

But if there is only one project  ---  if($totalRows_rsProjCount ==1)

then the details do not print. The file is still being included but with out the results. (I'm including the file below).

 

 

I'm not really sure what's happening here but the phrase while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets)); seems to kill the mysql_fetch_assoc...

 

is that right?

 

      <?php 
  //start repeat
  do {  
  ?>
  <tr class="d1">
    
      <td><strong><?php echo $row_rsTicketDets['airline']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['dateDepart']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['departingFrom']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['destination']; ?></strong></td>
    
    </tr>
<?php if ($totalRows_rsProjCount !=1){ 
    include('../travel/ticketPricing_projDets.inc.php');
} 
    //end repeat
} 	  while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets));

 if($totalRows_rsProjCount ==1){ 
 $row_rsTicketDets = mysql_fetch_assoc($rsTicketDets);
 include('../travel/ticketPricing_projDets.inc.php');
} ?>

 

ticketPricing_projDets.inc.php

    <tr>
      <td valign="top">Project Info:</td>
      <td colspan="2">
      <table width="100%" border="0">
        <tr>
          <td width="30%">Client:</td>
          <td><?php echo $row_rsTicketDets['ClientName']; ?></td>
        </tr>
        <tr>
          <td width="30%">Division:</td>
          <td><?php echo $row_rsTicketDets['Division']; ?></td>
        </tr>
        <tr>
          <td width="30%">Project:</td>
          <td><?php echo $row_rsTicketDets['projName']; ?></td>
        </tr>
        <tr>
          <td width="30%">Beginning Date:</td>
          <td><?php echo $row_rsTicketDets['projBegDate']; ?></td>
        </tr>
        <tr>
          <td width="30%">Ending Date:</td>
          <td><?php echo $row_rsTicketDets['projEndDate']; ?></td>
        </tr>
        <tr>
          <td width="30%">Location:</td>
          <td><?php echo $row_rsTicketDets['projLocation']; ?></td>
        </tr>
      </table>      </td>
      <td> </td>
      <td> </td>
    </tr>
    <tr>
      <td valign="top"><h3> </h3></td>
      <td><h3> </h3></td>
      <td><h3>Total for this Project:</h3></td>
      <td><h3>
        <input name="amt[<?php $row_rsTicketDets['ticketDetId'] ?>]" type="text" id="amt" size="8" />
      </h3></td>
      <td><h3> </h3></td>
    </tr>

 

you mean like this?

 

} 	  while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets))

 if($totalRows_rsProjCount ==1){ 

 

 

gives this error: Parse error: syntax error, unexpected T_IF, expecting ';' in \Travel\ticket_pricing.php on line 93

 

try if the fetch fetches data

while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets))

{

  print_r($row_rsTicketDets );//print the actual content of the array

    if($totalRows_rsProjCount ==1)

    {

 

I wish I could show you what is happening. It depends on where I put it. if I put it at the top, your code:

 

 	   //testing
while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets))
{
if($totalRows_rsProjCount ==1){
print_r($row_rsTicketDets );//print the actual content of the array
}    
}

returns an array: Array ( [airline] => Jefferson Airplane [dateDepart] => 2007-07-15 [departingFrom] => PDX [destination] => Seattle [dateReturn] => [amtUS] => [notes] => [projName] =>  [projBegDate] => 2006-02-01 [projEndDate] => 2006-12-31 [projLocation] => Portland [ClientName] => ACS [Division] => M&L )

 

 

But if I put it below the other mysql_fetch_assoc then it returns nothing.

 

This seems to confirm that you can only use the  function mysql_fetch_assoc once per page per query(?)

}   while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets));

 

if($totalRows_rsProjCount ==1){

$row_rsTicketDets = mysql_fetch_assoc($rsTicketDets);

include('../travel/ticketPricing_projDets.inc.php');

} ?>

 

explain what you want with each line that may help me help you

     <?php 
  //start repeat
  do {  
  // the next row(s) print no matter what
?>
  <tr class="d1">
    
      <td><strong><?php echo $row_rsTicketDets['airline']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['dateDepart']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['departingFrom']; ?></strong></td>
      <td><strong><?php echo $row_rsTicketDets['destination']; ?></strong></td>
    
    </tr>
<?php 
    //if there are multiple projects then print out each detail. this works fine
    if ($totalRows_rsProjCount !=1){ 
    include('../travel/ticketPricing_projDets.inc.php');
} 
    //end repeat
} 	  while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets));

 if($totalRows_rsProjCount ==1){ 
   //still working to this point
   //now I'm trying to turn the association back on. this next line is evidence of a total hack attempt
 $row_rsTicketDets = mysql_fetch_assoc($rsTicketDets);
   //now I want to include the file only once because there is only one project detail. 
   //there may have been multiple "legs" on the itinerary but all the same project so we only need the project detail info once.
   //The include works but the query is no longer populating the page. My guess is because mysql_fetch_assoc is broken down. 
 include('../travel/ticketPricing_projDets.inc.php');
} ?>

<?php

//start repeat

while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets))

{

echo" <tr class="d1">

      <td><strong>".$row_rsTicketDets['airline']."</strong></td>

      <td><strong>".$row_rsTicketDets['dateDepart']."</strong></td>

      <td><strong>". $row_rsTicketDets['departingFrom']."</strong></td>

      <td><strong>".$row_rsTicketDets['destination']."</strong></td>

    </tr>";

  if($totalRows_rsProjCount ==1){

include('../travel/ticketPricing_projDets.inc.php');

  }

    else{ //i dont understand this but you say its fine so be it

    include('../travel/ticketPricing_projDets.inc.php');

}

}

 

so thats my version i havent run that but inform me if there error

 

thanks for trying. now instead of printing 2 rows in the loop it only prints one.

 

So you might understand better what I'm trying to do: Let's say you are going to Mexico and then to the U.S. for a project called 'Dance Master'

 

I expect the print out to be:

 

Itinerary:

To: Mexico  July 21

To: U.S. July 28

 

Project details:

Dance Fever

Mexico City

July 1-August 15

 

 

Conversely, If there is another project you're working on in the U.S. before coming home, I want to print out like such:

 

Itinerary:

To: Mexico  July 21

Project details:

Dance Fever

Mexico City

July 1-August 15

 

To: U.S. July 28

Project details:

Mad games

Los Angeles

July 23-30

mysql_select_db($database_conn_org, $conn_org);
$query_rsTicketDets = "SELECT itinerary_dets.airline, itinerary_dets.dateDepart, itinerary_dets.departingFrom, itinerary_dets.destination, itinerary_dets.dateReturn, itinerary_dets.amtUS, itinerary_dets.notes, projects.projName, projects.projBegDate, projects.projEndDate, projects.projLocation, tblclients.ClientName, tbldivisions.Division FROM itinerary_dets Inner Join projects ON itinerary_dets.projId = projects.projID Inner Join contracts ON projects.contractID = contracts.contractID Inner Join tblclients ON contracts.clientID = tblclients.ClientID Inner Join tbldivisions ON contracts.divisionID = tbldivisions.DivID WHERE itinerary_dets.itineraryId='$ticket'";
$rsTicketDets = mysql_query($query_rsTicketDets, $conn_org) or die(mysql_error());
$row_rsTicketDets = mysql_fetch_assoc($rsTicketDets);
$totalRows_rsTicketDets = mysql_num_rows($rsTicketDets);

 

sorry. had to drive home. hope your still around. thanks

In case anyone is wondering, I figured this out. I needed a mysql_data_seek in order to reset the row.

This is the working code.

<?php 
    //if there are multiple projects then print out each detail. this works fine
    if ($totalRows_rsProjCount !=1){ 
    include('../travel/ticketPricing_projDets.inc.php');
} 
    //end repeat
} 	  while ($row_rsTicketDets = mysql_fetch_assoc($rsTicketDets));

 if($totalRows_rsProjCount ==1){ 
//reset the row  
mysql_data_seek($rsTicketDets, 0);

  //turn association back on
 $row_rsTicketDets = mysql_fetch_assoc($rsTicketDets);
   //now I want to include the file only once because there is only one project detail. 
   //there may have been multiple "legs" on the itinerary but all the same project so we only need the project detail info once.

 include('../travel/ticketPricing_projDets.inc.php');
} ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.