Pulling data and displaying it in tables....

Well i was doing so well, then i got stuck… :smiley:

Oh, i do intend on creating a seperate file for all these database connections i’m making, for now i just want to get it working, i can tweak it all later on when i become a bit more competent.

Thanks

<?php
session_start();

  // Are we logged in?
    if (!isset($_SESSION['db_is_logged_in']) 
      || $_SESSION['db_is_logged_in'] !== true) {

   // If we're not logged in, piss off to the login page
   header('Location: login.php');
   exit;
}

  // Connect to the database and run the query
  $dbid = mysql_connect ('localhost', 'username, 'password');
	          mysql_select_db('database',$dbid) 
	          or die ("Cannot find database");
	
	  $query = "SELECT * FROM request_data";
	  $result = mysql_query($query,$dbid) 
	    or die("Select error:".mysql_error());

  // Fetch the data from the selected table
  while($r=mysql_fetch_array($result))
  {	
        
   // Put the data from the query into the following variables
   $req_id=$r["req_id"];
   $manufacturer=$r["manufacturer"];
   $model=$r["model"];
   $capacity=$r["capacity"];
   $year=$r["year"];
   $part_one=$r["part_one"];
   $part_two=$r["part_two"];
   $part_three=$r["part_three"];
   $part_four=$r["part_four"];
   $part_five=$r["part_five"];
   $full_name=$r["full_name"];
   $telephone=$r["telephone"];
   $alt_telephone=$r["alt_telephone"];
   $email=$r["email"];
   $contact_pref=$r["contact_pref"];
   $area=$r["area"];
   $timestamp=$r["timestamp"];
   $comments=$r["comments"];
   
   
   //display the row
  // this is working so we know we just need to fix the tables
  //echo "$req_id <br>";
  }

?>

<html>
<head>
<title>Secure Area</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

  <!-- This is being commented out because it's not what we want -->
  <!--<table width="600" height="100%">
    <tr>
      <td>Req ID</td><td>Manufacturer</td><td>Model</td><td>Capacity</td><td>Year</td>
      <td>Part One</td><td>Part Two</td><td>Part Three</td><td>Part Four</td><td>Part Five</td>
      <td>Name</td><td>Telephone</td><td>Alt Telephone</td><td>Email</td>
      <td>Contact Pref</td><td>Area</td><td>TimeStamp</td><td>Comments</td>
    </tr>
    <tr>
      <td><?php echo "$req_id" ?></td><td><?php echo "$manufacturer" ?></td><td><?php echo "$model" ?></td><td><?php echo "$capacity" ?></td><td><?php echo "$year" ?></td>
      <td><?php echo "$part_one" ?></td><td><?php echo "$part_two" ?></td><td><?php echo "$part_three" ?></td><td><?php echo "$part_four" ?></td><td><?php echo "$part_five" ?></td>
      <td><?php echo "$name" ?></td><td><?php echo "$telephone" ?></td><td><?php echo "$alt_telephone" ?></td><td><?php echo "$email" ?></td>
      <td><?php echo "$contact_pref" ?></td><td><?php echo "$area" ?></td><td><?php echo "$timestamp" ?></td><td><?php echo "$comments" ?></td>
    </tr>
  </table>-->
  



<body>
<!--<p>This is where we will read the submissions and send them out. </p>-->
<p>&nbsp;</p>
<p><a href="logout.php">Logout</a> </p>
</body>
</html>

If I am understanding you correctly, you want a new table for ever row of in the database?
[php]<?
while($r=mysql_fetch_array($result))
{

// Put the data from the query into the following variables
$req_id=$r[“req_id”];
$manufacturer=$r[“manufacturer”];
$model=$r[“model”];
$capacity=$r[“capacity”];
$year=$r[“year”];
$part_one=$r[“part_one”];
$part_two=$r[“part_two”];
$part_three=$r[“part_three”];
$part_four=$r[“part_four”];
$part_five=$r[“part_five”];
$full_name=$r[“full_name”];
$telephone=$r[“telephone”];
$alt_telephone=$r[“alt_telephone”];
$email=$r[“email”];
$contact_pref=$r[“contact_pref”];
$area=$r[“area”];
$timestamp=$r[“timestamp”];
$comments=$r[“comments”];

//display the row
// this is working so we know we just need to fix the tables
//echo "$req_id
";
?>

Req ID Manufacturer Model Capacity Year Part One Part Two Part Three Part Four Part Five Name Telephone Alt Telephone Email Contact Pref Area TimeStamp Comments
<?php echo "$req_id" ?> <?php echo "$manufacturer" ?> <?php echo "$model" ?> <?php echo "$capacity" ?> <?php echo "$year" ?> <?php echo "$part_one" ?> <?php echo "$part_two" ?> <?php echo "$part_three" ?> <?php echo "$part_four" ?> <?php echo "$part_five" ?> <?php echo "$name" ?> <?php echo "$telephone" ?> <?php echo "$alt_telephone" ?> <?php echo "$email" ?> <?php echo "$contact_pref" ?> <?php echo "$area" ?> <?php echo "$timestamp" ?> <?php echo "$comments" ?>
<? } ?>[/php]

PS: You can never ask for to much help when you are showing us you are making effort in doing things yourself!

Right, after doing a bit of reading it didn’t take long to work out how it’s done…

Here is the code… One thing i’m wondering, this table isn’t displaying all the info from the table, how would i add a clickable link that when clicked would bring up the selected entry in the database and show the whole lot on a seperate page?

[php]

<?php session_start(); // Are we logged in? if (!isset($_SESSION['db_is_logged_in']) || $_SESSION['db_is_logged_in'] !== true) { // If we're not logged in, piss off to the login page header('Location: login.php'); exit; } // Connect to the database and run the query $dbid = mysql_connect ('localhost', 'username', 'password'); mysql_select_db('database',$dbid) or die ("Cannot find database"); $query = "SELECT * FROM request_data"; $result = mysql_query($query,$dbid) or die("INSERT error:".mysql_error()); // Fetch the data from the selected table echo "";while($row = mysql_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Request ID Manufacturer Model Year Part 1 Name Telephone Email Area Time
" . $row['req_id'] . "" . $row['manufacturer'] . "" . $row['model'] . "" . $row['year'] . "" . $row['part_one'] . "" . $row['full_name'] . "" . $row['telephone'] . "" . $row['email'] . "" . $row['area'] . "" . $row['timestamp'] . "
";mysql_close($con); ?> Secure Area

 

Logout

[/php]

First off…

For the previous code…
I prefer to hop in and out of PHP. Its makes it easier to read in most cases and I have done several tests that shows that is faster running the page through the parser with the least amount of HTML going through the PHP parser.
For example:
[php]<?
while($row = mysql_fetch_array($result))
{
?>

<?php echo $row['req_id']; ?>
<?php echo $row['manufacturer']; ?>
<?php echo $row['model']; ?>
//SO ON AND SO FORTH…

}
?>
[/php]

I would say however in this case the way you have it is fine… Now for the problem at hand. Here is what I like to do:

I normally make a seperate page like view.php and then create a link as such:

[php]
See More
[/php]

Then on view.php page you can simply do:

[php]<?php
$reg_id = $_GET[‘id’];

//THEN WRITE QUERY PULLING INFORMATION BASED ON THAT ID
//THEN OUTPUT AND FORMAT THAT INFORMATION PULLED FROM DATABASE
[/php]

And your done! :)

That all makes sense, and shouldn’t be too diffiult, but the page isn’t loading when i do the following:

*Note: for some reason the forum is removing the closing php tag in between the tags. It is there it’s just the forum isn’t displaying it…

[php]
echo "




";while($row = mysql_fetch_array($result))
{
echo “”;
echo “”;
echo “”;
}
echo “
Request ID
” . <?php echo "$row['req_id']"; ?> . “
”;mysql_close($con);

[/php]

First off, you have a mistake in your echo statement. You don’t have the <a tag encapsulated with double quotes which brings me to my next statement…
As I stated before, its best to keep HTML out of the paser especially in this case with using hyperlinks compare the 2 examples:

[php]

<?php while($row = mysql_fetch_array($result)) { ?>
  <tr>
 <td><a href="view.php?id=<?php echo  $id; ?>" target="_blank"> <?php echo $req_id;?> </a></td>
  </tr>
<?php } ?>

// or the same thing - Only running the HTML through the parser which most programmers will agree is best NOT to do.

<?php while($row = mysql_fetch_array($result)) { echo ""; echo "" . $row['req_id'] . ""; echo ""; } ?>

[/php]

If for no other reason, I would simply opt for jumping in/out of php because of readablity and how confusing escaping double quotes and such can be.

2 more posibilities:
[php]

<?php while($row = mysql_fetch_array($result)) { echo ''; echo '' . $row['req_id'] . ''; echo ''; } ?> <?php while($row = mysql_fetch_array($result)) { echo ""; echo "$row[req_id]"; echo ""; } ?>

[/php]

single quotes remove the need of escape the double quotes.

everbody peferes one of them.

i think the most impotent is to use only one of them. and not mix them without a very good reason.

Q1712 is correct, but both ways will run HTML through the paser which is in general not seen as good practice. At least from the other people I have talked to which do include Authors of books and various others on different forums.

Along with the fact that I have done serveral tests on this and it also slows load times, again, nothing anyone would ever notice, but it still fact remains it is slower.

With all that being said, do as you feel most comfortable doing.

as we are now dicusing that topic, a little overview:

  1. ?> ... <?php echo $var ?> ... <?php:
  • fastest,
  • considered the best by most php/html-programmers
  • creates more traffic (most of the times more blanks are outputted)
  • created the best readable html source
  1. echo "...".$var."...":
  • little slower
  • but the " are confusing
  • more related to the style used in real programing languages (e.g. C, C++, Java …) and PERL.
  1. echo '...'.$var.'...':
  • as fast as 2.
  • no need to escape double quotes
  • more related to the style used in real programing languages (e.g. C, C++, Java …) and PERL.
  1. echo "...$var...":
  • slowest (up to 3 times as slow as 1.)
  • hardest to debug
  • more related to the style used in PERL.
  • i never use it.

alway use only one of them

P.S.: actually i won’t consider Java to be a real programming language

I agree on all points… including the JAVA!! :D

Oh, just incase anyone cared, for giggles, I created a test page which outputs a small html form 20 times, once running the html through the parser each time and then one not running the html through and the results were as follows:

0.000473 seconds - RUNNING HTML THROUGH PASER
0.000295 seconds - NOT RUNNING HTML THROUGH PASER

as u already wrote the script. may i ask u to if u used any dots to combine strings? and if u could rerun it with commas instead of the dots?

i would like to know if a significant part of that time is used by combining an creating a copy of strings.

maybe the last posts should be moved to a new topic?

Good Idea on the moving discussion:

Moved to: http://www.phphelp.com/forums/viewtopic.php?t=7488

Beauty is in the eye of the beholder I guess. Jumping in and out of PHP makes it less readable/debuggable to me to be honest.

I usually use this to be honest. Especially when syntax highlighting is enabled this is very convenient.

In fact, I dare call it faster, because strings between single quotes are not checked by the parser for variables :wink:

I think this is generally not a good way to write code, as it confuses what’s part of the variable and what’s not in some cases (especially with text that connects without a space this can become unreadable).

Amen, consistency is a blessing.

Lol, and PHP is? Seriously, if I had to write a BIG project I’d opt for Java over PHP any day. It’s faster, better supported, more custom modules available, very strict and strong-type, etc. etc. I’m a software developer for a company that specializes in EMC software called Documentum (which has been written entirely in Java), so I know what I’m talking about.

this i about real programing languages (C, C++, Pascal/Delphi, even Lisp, …) vs Java.
PHP is a script language.

right J2EE much better than PHP for big projects.
but if i have the choise, i always choose C++ instead of Java for non web applications.

Well yea, but C++ isn’t good for mixing web and non-web apps afaik. Languages such as Java and Python are better for that (though Python is relatively new and kinda gives me the same feeling as PHP does).

Sponsor our Newsletter | Privacy Policy | Terms of Service