Jump to content

Reading a table, and manipulating the data in rows in a table...


diode

Recommended Posts

Hello, and how are you?

I want to thank everyone who has tried to help me in the past. Unfortunately, there is another problem I have run into.

What I want to do is read data from table msgs, which has 4 fields: postno, acctname, dateposted, and message.

I am trying to display the acctname and dateposted in their own row, and then display the message into its own row below it, thereby making each post in the board that I'm trying to make - result in 2 rows.

What I have does not work. I was wondering how a person would do this.

Here is my current code. If you like, you might wish to create your own compatible table to mess around with this so that you can see what I'm doing.[code]<?php  session_start();

$con = mysql_connect('deleted to protect security', 'deleted to protect security', 'deleted to protect security'
  or die('Could not connect: ' . mysql_error());

echo 'Connected successfully';
mysql_select_db('test', $con) or die('Could not select database');

$sql="select count(*) from msgs";
$qry=mysql_query($sql) or die (mysql_error()); # here i am assuming that you had only one row.
$result=mysql_fetch_array($qry);
$line=1;

$rows=$result['count(*)'];

//mysql_free_result($result);

echo '<table style="text-align: left; font: 9pt Arial; width: 605px; height: 76px;" border="0" cellpadding="8" cellspacing="2">';

while ($line >= $rows) {
 
  $sql="select postno, acctname, dateposted, message from msgs where postno = $line";
  $qry=mysql_query($sql) or die (mysql_error()); # here i am assuming that you had only one row.
  $result=mysql_fetch_array($qry);
 
  $post=$result['postno'];
  $acctname=$result['acctname'];
  $dateposted=$result['dateposted'];
  $message=$result['message'];
 
  echo "\t<tr bgcolor=#8888ff>\n";
  echo "<td><b>From: </b> $acctname |  <b>Posted: </b> $dateposted</td>";
  echo "\t</tr>\n";

  echo '<tr bgcolor="#b0b0ff">';
  echo "<td>$message</td>";
  echo "</tr>";

  $line++;
  }

  echo "</table>";

//mysql_free_result($result);

mysql_close($con);
?>
[/code]

As always, thanks in advance :D,
diode
Hi there.

You only need to make one MySQL call to achieve what you want - and it's probably more efficient.  This is how I'd do it.

[code]
<?php 

session_start();

$con = mysql_connect('deleted to protect security', 'deleted to protect security', 'deleted to protect security'
  or die('Could not connect: ' . mysql_error());

echo 'Connected successfully';
mysql_select_db('test', $con) or die('Could not select database');

echo '<table style="text-align: left; font: 9pt Arial; width: 605px; height: 76px;" border="0" cellpadding="8" cellspacing="2">';

$sql="select * from msgs";
$qry=mysql_query($sql) or die (mysql_error());

while ($row = mysql_fetch_array($qry, MYSQL_ASSOC))
{
$post=$row['postno'];
$acctname=$row['acctname'];
$dateposted=$row['dateposted'];
$message=$row['message'];
 
echo "\t<tr bgcolor=#8888ff>\n";
echo "<td><b>From: </b> $acctname |  <b>Posted: </b> $dateposted</td>";
echo "\t</tr>\n";

echo '<tr bgcolor="#b0b0ff">';
echo "<td>$message</td>";
echo "</tr>";
}

echo "</table>";

mysql_free_result($qry);
mysql_close($con);
?>
[/code]

Hope that helps :)

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.