Jump to content

MySQL Query - Display Issue/Possible HTML?


abjones116

Recommended Posts

Hello All-

 

I am trying to pull some information from a database. I am successfully the information, however, when in the table - the first row of the table (HTML) appears correctly, but any subsequent rows of data on the website are all jammed together on the same row. Hopefully - that makes sense... I was using a printf function but I changed it to see if that was the underlying issue - You will be able to see that with the comments in the HTML.

 

 

<p><strong>Membership 
											Transaction History:</strong></p>
<p> </p>
											<table style="width: 100%" cellpadding="0" cellspacing="0" align="center">
												<tr>
													<td><strong>Date</strong></td>
													<td><strong>Transaction ID</strong></td>
													<td><strong>Method</strong></td>
													<td><strong>Amount</strong></td>
													<td><strong>Status</strong></td>
												</tr>


<?
/*   Get Transactions For Current User   */
$t_select	= "SELECT * FROM `transactions` WHERE `m_id`='".$sm_id."' ORDER BY time DESC";
$t_result	= mysql_query($t_select) or die(mysql_error());

if (mysql_num_rows($t_result)== "0")   {
  echo "</table><br><p align=\"center\">No Transactions Available.</p>";
  
}   else   {

  while($t_row = mysql_fetch_array($t_result)) {
  
$txn_time   =   $t_row['time'];
$date     	=   date('m/d/Y', strtotime($txn_time));

  
?>
  
  <tr>
  	<td><? echo $date;?></td>
  	<td><? echo $t_row['transaction_id'];?></td>
  	<td><? echo $t_row['method'];?></td>
  	<td><? echo $t_row['amount'];?></td>
  	<td><? echo $t_row['status'];?></td>
  </tr>
  
<?
    //$txn_time   =   $t_row['time'];
    //$format     =   "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>";
    //$date     	=   date('m/d/Y', strtotime($txn_time));
    //printf($format, $date, $t_row['transaction_id'], $t_row['method'], $t_row['amount'], $t_row['status']);
  	?></table><?  
  }   
}
?>

 

I appreciate everyone's help.

Link to comment
Share on other sites

Yes, PHP prints exactly what you ask it to.  While it may show a single line in the source, the browser will read the HTML, and output it correctly.  If this is still an issue, you can look into adding line breaks with \n or \r\n.  Or, you could use the php constant PHP_EOL.

 

<?php
//examples.
echo 'Create a line break' . "\n" . 'before this line!'; //notice that the line break must be enclosed in double quotes.
echo "Yes it will\nwork here too";
echo "This will" . PHP_EOL . 'also work';

Link to comment
Share on other sites

Yes, PHP prints exactly what you ask it to.  While it may show a single line in the source, the browser will read the HTML, and output it correctly.  If this is still an issue, you can look into adding line breaks with \n or \r\n.  Or, you could use the php constant PHP_EOL.

 

@jcbones: Although, you are correct that the source would be on one line and that is a HUGE pet peeve of mine. But, that is not the problem. I don't think the OP really understood the question from Katten

 

@abjones116:

 

It would have helped if you had posted some of the HTML source in question. But, looking at the code it appears you are opening the table before you start the loop. Then, in the loop, you are putting a closing table tag after each record. So, after the the first record , and the first closing table tag, the browser doesn't know how to interpret the subsequent TR and TD tags since they are not within a table. Try to be more organized in the logic and format of your code so these problems won't occur. Also, don't use PHP short tags (i.e. "<?") - use the full PHP tag - "<?php".

 

I would also suggest putting the "logic" of your code (i.e. the core PHP code) at the top of your script. Use it to generate the output needed. Then put all the output (i.e. HTML) at the bottom of the script using echo's of the variables you created previously. The code below should work - not tested for minor syntax errors.

 

<?php

/*   Get Transactions For Current User   */
$query = "SELECT *
          FROM `transactions`
          WHERE `m_id`='{$sm_id}'
          ORDER BY time DESC";
$result = mysql_query($query);

//Create variable to hold transaction output
$txOutput '';

if(!$result)
{
    //Use first line for debugging or 2nd line for production
    $txOutput = "    <tr><td colspan='5'><b>Query failed!</b><br>Query: $query<br>Error: " . mysql_error() . "</td></tr>\n";
    //$txOutput = "    <tr><tdcolspan='5'>Unable to get transactions. If the problem continues contact the administrator.</td></tr>\n";
}
if (!mysql_num_rows($result))
{
    $txOutput = "    <tr><td colspan='5'>No Transactions Available.</td></tr>\n";
}
else
{
    while($row = mysql_fetch_array($result))
    {
        $date =   date('m/d/Y', strtotime($row['time']));
        $txOutput .= "    <tr>\n";
        $txOutput .= "        <td>{$date;}</td>\n";
        $txOutput .= "        <td>{$row['transaction_id']}</td>\n";
        $txOutput .= "        <td>{$row['method']}</td>\n";
        $txOutput .= "        <td>{$row['amount']}</td>\n";
        $txOutput .= "        <td>{$row['status']}</td>\n";
        $txOutput .= "    </tr>\n";
    }
}
?>
<html>
<body>

<p><strong>Membership Transaction History:</strong></p>
<p> </p>

<table style="width: 100%" cellpadding="0" cellspacing="0" align="center">
    <tr>
        <th><strong>Date</strong></th>
        <th><strong>Transaction ID</strong></th>
        <th><strong>Method</strong></th>
        <th><strong>Amount</strong></th>
        <th><strong>Status</strong></th>
    </tr>
    <?php echo $txOutput; ?>
</body>
</html>

 

Edit: Note the spaces I have included in the output to pad the left side of the HTML code. Also, at the end of each line I used a \n (this will create a line break in the HTML source). Doing things like this will make it SIGNIFICANTLY easier to view the source of your rendered documents and find/fix problems. Note that the \n must be used within a double quoted string.

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.