11 Replies - 1270 Views - Last Post: 05 September 2002 - 06:43 PM Rate Topic: -----

#1 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Processing Recordsets

Posted 05 September 2002 - 05:30 PM

let's say I have something like this:

$strSQL = "SELECT * FROM place ORDER BY placeID";
$result = mysql_query($strSQL);

echo("<table border=\"1\" cellpadding=\"3\">");
echo("<tr><th>ID</th><th>City</th><th>Region</th><th>Country</th><th>Continent</th></tr>");

while ($row = mysql_fetch_array($result)) {
	echo("<tr><td>".$row["placeID"]."</td><td>".$row["city"]."</td>");
	echo("<td>".$row["region"]."</td><td>".$row["country"]."</td>");
	echo("<td>".$row["continent"]."</td></tr>");
}



Is there a way to automate the stuff in the while loop (maybe with a for loop?) so that I don't have to type out each column name, and just put a <td> around each one? I'm just not sure how to reference that....

Is This A Good Question/Topic? 0
  • +

Replies To: Processing Recordsets

#2 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Processing Recordsets

Posted 05 September 2002 - 05:37 PM

i'm gonna have to think on this, but in the meantime, here's a tip, you don't have to do this thing:

$row["placeID"]


you can do the same thing with

extract($row);


and then you could just say $variable_name where-ever you wanted
Was This Post Helpful? 0
  • +
  • -

#3 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Re: Processing Recordsets

Posted 05 September 2002 - 05:42 PM

nifty. save a bit of typing. :)
Was This Post Helpful? 0
  • +
  • -

#4 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Processing Recordsets

Posted 05 September 2002 - 05:44 PM

as far as the <td> stuff is concerned, i think you're stuck doing it the way you've got it because you've got a diff variable name within each td...

*thinks some more*
Was This Post Helpful? 0
  • +
  • -

#5 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Re: Processing Recordsets

Posted 05 September 2002 - 05:49 PM

I guess what I need to do is find out how many elements are in that array ($row), and then just list them all, adding tags as I go. I could use the index number instead of the name. That sounds simple enough but I don't know enough php. :)
Was This Post Helpful? 0
  • +
  • -

#6 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Processing Recordsets

Posted 05 September 2002 - 05:56 PM

just keep asking questions, we'll help you out
Was This Post Helpful? 0
  • +
  • -

#7 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Re: Processing Recordsets

Posted 05 September 2002 - 06:21 PM

alright, I discovered count() and did it like this:

while ($row = mysql_fetch_array($result)) {
	echo("<tr>");
	for ($i=0; $i<(count($row)/2); $i++) {
  echo("<td>".$row[$i]."</td>");
	}
	echo("</tr>");
}



which is sweet... cuz I have to do a whole bunch of tables that way and this saves me from making them all individually. :)

edit: I just added the /2 because I ended up with extra empty cells at the end, so it looks like it was counting both the keys and the values! good to know.
Was This Post Helpful? 0
  • +
  • -

#8 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Processing Recordsets

Posted 05 September 2002 - 06:25 PM

well check that out, i forgot about count()...well, that's probably because i never used it much, see, we learn something new everyday...
Was This Post Helpful? 0
  • +
  • -

#9 fyrestorm  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 10
  • View blog
  • Posts: 3,113
  • Joined: 04-April 02

Re: Processing Recordsets

Posted 05 September 2002 - 06:27 PM

well actually, if we don't learn something new then we at least revisit something we learned another day ago...glad you reminded me of count()
Was This Post Helpful? 0
  • +
  • -

#10 Cookie Mobster  Icon User is offline

  • nooneenooneenooonee
  • member icon

Reputation: 7
  • View blog
  • Posts: 4,730
  • Joined: 12-October 01

Re: Processing Recordsets

Posted 05 September 2002 - 06:36 PM

Just a few things... using the count func repetedly will be hard on the server try this (ok only one thing :))
while ($row = mysql_fetch_array($result)) {
echo("<tr>");
foreach($row as $field){
  echo("<td>".$field."</td>");
}
echo("</tr>");
}



Cheers Sam ;)
Was This Post Helpful? 0
  • +
  • -

#11 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Re: Processing Recordsets

Posted 05 September 2002 - 06:40 PM

that doesn't work, though... for some reason it's doing each one twice, so I'm getting twice as many rows as I want... any idea why?
Was This Post Helpful? 0
  • +
  • -

#12 klewlis  Icon User is offline

  • cur tu me vexas?

Reputation: 8
  • View blog
  • Posts: 1,723
  • Joined: 09-November 01

Re: Processing Recordsets

Posted 05 September 2002 - 06:43 PM

how about this instead:

while ($row = mysql_fetch_array($result)) {
	$upper = count($row)/2;
	echo("<tr>\n");
	for ($i=0; $i<$upper; $i++) {
  echo("<td>".$row[$i]."</td>");
	}
	echo("\n</tr>\n");
}



then it only counts once per row...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1