Help with mysql in tables.

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 1468 Views - Last Post: 06 December 2010 - 05:52 PM Rate Topic: -----

#1 indysolo621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Help with mysql in tables.

Posted 05 December 2010 - 11:20 AM

I am trying to get it to show the mysql data each in seperate tables on the page instead of all in one. The code I have is fine except for the fact that it puts all the data in 1 table. How would I get it to do it all in seperate tables?

<?
session_start();
require "header.php";
require_once("connect.php");
if (!empty($_SESSION['username'])) // he got it.
{
$Data2= mysql_query("SELECT `price`,`id`,`item`,`image`,`rent` FROM `items`") or die(mysql_error());
while ($Data3 = mysql_fetch_array($Data2))
{
print"
<center><table border=1><tr><td><center><a href='buy.php?ID=".$Data3['id']."'><img src='buy.png' width='50' height='15' /></a></center> <br/><center><img src='".$Data3['image']." /></center></center><br/><center><b>Name:</b>".$Data3['item']."</center><br/><center><b>Rent:</b>".$Data3['rent']."</center><br/><center><b>Price:</b>".$Data3['price']."</center></td></tr></table><br/>
";
}
}
if (!($_SESSION['username'])) // bad info.
{
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
}
require "footer.php";
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Help with mysql in tables.

#2 nahtanoJ  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-September 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 11:42 AM

Try using

while ($Data3 = mysql_fetch_array($Data2, MYSQL_ASSOC))

You can reference the array element by their data field name
Was This Post Helpful? 0
  • +
  • -

#3 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 12:07 PM

or simply use while ($data3=mysql_fetch_assoc($result)) {
Was This Post Helpful? 0
  • +
  • -

#4 indysolo621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 12:47 PM

I tried both of those and they all still show up in one table. By table I mean the <tr></tr>. The actualy text part of the code that you see on the webpage. How would you fix that?
Was This Post Helpful? 0
  • +
  • -

#5 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 12:54 PM

Give this a try...


<?
  session_start();
  require "header.php";
  require_once("connect.php");
  if (!empty($_SESSION['username'])) {
?>
<center>
<table border=1>
<?php
    $Data2 = mysql_query("SELECT `price`,`id`,`item`,`image`,`rent` FROM `items`") or die(mysql_error());
    while ($Data3 = mysql_fetch_assoc($Data2)) {
?>
<tr>
<td><center><a href='buy.php?ID=<?php echo $Data3['id']; ?>'><img src='buy.png' width='50' height='15' /></a></center>
<br/><center><img src='<?php echo $Data3['image']; ?>' /></center></center><br/><center><b>Name:</b><?php echo $Data3['item']; ?></center>
<br/><center><b>Rent:</b><?php echo $Data3['rent']; ?></center><br/><center><b>Price:</b><?php echo $Data3['price']; ?></center></td>
</tr>
<?php
    }
  }
?>
</table>
<?php
  if (!($_SESSION['username'])) {
	echo "<center>You are currently <b>NOT</b> logged in. Please login. </center>";
	echo "<meta http-equiv=\"REFRESH\" content=\"0;url=login.php\">\n";
  }
  require "footer.php";
?>


Was This Post Helpful? 1
  • +
  • -

#6 indysolo621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 12:57 PM

works perfectly :) thank you very much :) how would I get the tables to show side by side like left to right instead of ontop of one another?
Was This Post Helpful? 0
  • +
  • -

#7 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 01:00 PM

a mixture of </tr><tr> and counting how many columns that you have done so far.
Was This Post Helpful? 0
  • +
  • -

#8 indysolo621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 29-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 01:02 PM

ok, thanks :)
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2930
  • View blog
  • Posts: 10,133
  • Joined: 08-August 08

Re: Help with mysql in tables.

Posted 05 December 2010 - 02:39 PM

Do not use tables for formatting! That's just begging for trouble. If you need two tables to be side by side, put each in a div and use css to position those.

And separate your code! You shouldn't break out of php more than once or twice in a few hundred lines of code. You do it eight times in less than thirty! If/when your code gets to more than 100 lines you're going to have no idea how to manage it.
Was This Post Helpful? 0
  • +
  • -

#10 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:10 PM

You make it sound wrong, everybody has their own way of scripting something. It isn't like it's going to stop working.

The ways that you're suggesting you'd end up with so many files going on, you wouldn't know what the heck you're doing any more.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2930
  • View blog
  • Posts: 10,133
  • Joined: 08-August 08

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:20 PM

I suggest you spend some time looking through the help requests here. For every one with multiple files there are dozens, if not hundreds with code like the above. That's because with multiple files each containing their own language it's easy to know what you're editing. With your code, I couldn't tell how many breaks out of php you had without doing a search for ?> in the text!
Was This Post Helpful? 0
  • +
  • -

#12 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:21 PM

Maybe not you, no. Like I said, each to their own.

After all so long as it works, it is not wrong.
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2930
  • View blog
  • Posts: 10,133
  • Joined: 08-August 08

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:24 PM

Oh, and "it works" is the least important factor when determining code quality. Of course it has to work! To be any good it has to work and be editable.

View PostKrammeh, on 05 December 2010 - 05:21 PM, said:

After all so long as it works, it is not wrong.

It's that attitude that gives php a bad reputation with programmers in other languages.
Was This Post Helpful? 0
  • +
  • -

#14 Krammeh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 15-November 10

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:29 PM

Okay, if you're going to be like that then.

What if the site has 2500 dynamic pages on it. You end up with what? 15 files per dynamic page (due to the output of each section)? So that's 37,500 other scripts that you're going to be including into the code just to make it more readable.

That is going to cause massive slow downs with disk seek, not to mention going to be a headache to download the list of files through the FTP explorer.
Was This Post Helpful? 0
  • +
  • -

#15 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2930
  • View blog
  • Posts: 10,133
  • Joined: 08-August 08

Re: Help with mysql in tables.

Posted 05 December 2010 - 03:44 PM

You're not understanding what php is for. You could easily have thousands of different pages with just a few html pages because php can alter them on the fly. That means fewer files accessed, not more. Fewer html files, fewer php files, and they're all easily edited.

Your code on the other hand will be unmanageable with less than a hundred additional lines of code added. Forget about multiple pages! Honestly, it's a nightmare waiting to happen. I routinely have to clean up after code like yours because the original coders have given up (for giving up, that is). Can't say that I blame them. I often have to rewrite it from scratch because that's the easier, more cost effective approach!

This post has been edited by CTphpnwb: 05 December 2010 - 03:45 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3