5 Replies - 4204 Views - Last Post: 27 November 2008 - 07:46 PM Rate Topic: -----

#1 cookied89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 08

PHP Query While Loop Help

Posted 25 November 2008 - 06:23 PM

Hi,

Is there a way to increment the value on these ?
Like I run a query,

<? $query = "SELECT * from products WHERE `this` = 'that'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){

<input type="text" name="X1" size="20" value="
<input type="text" name="Y1" size="20">

}


See Where I have X1 and Y1,
well For Result one I want it to be called that,

and for the next one I want it to X2, Y2,
etc.

I know that code isn't exactly correct and won't work without escaping characters,
but just looking for the idea if this is possible

Oh and lol @

Posted Image

I'm sure they look like that. :D

Is This A Good Question/Topic? 0
  • +

Replies To: PHP Query While Loop Help

#2 Mike007  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 7
  • View blog
  • Posts: 332
  • Joined: 30-August 07

Re: PHP Query While Loop Help

Posted 25 November 2008 - 07:03 PM

Yeah it's not correct but with a few modifications it can work. I wouldn't suggest doing that way, it's much better to use a template but for something small, or where you can't really use a template (e.g. an install script, since you don't have access to a template engine before a few simple things are set up). Anyway:

<?php 
$query = "SELECT * from products WHERE `this` = 'that'";
$result = mysql_query($query) or die(mysql_error());
$count = 0;
while($row = mysql_fetch_array($result))
{
$count++;
?>
<input type="text" name="X<?php echo $count ?>" size="20" value="">
<input type="text" name="Y<?php echo $count ?>" size="20">
<?php
}
?>



This should work fine, the reason it works is that PHP is a scripting language and it is executed line by line.. Therefore it goes back to the same line number after finishing everything inside the while loop. When it encounters HTML it just spits it out to the output stream.

I've seen this done before, but it is quite ugly as you can see, and if there is a lot of code and a lot of HTML, things tend to look even worst.

Good luck.
Was This Post Helpful? 0
  • +
  • -

#3 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: PHP Query While Loop Help

Posted 25 November 2008 - 07:13 PM

Not sure if the above code is what you are looking for....

here:

<?php

$query = "SELECT * from products WHERE `this` = 'that'";
$result = mysql_query($query);
$numrows = mysql_num_rows($result);
$count=0;

while($count > $numrows){

echo "<input type=\"text\" name=\"X\"". $count ."\" size=\"20\">";
echo "<input type=\"text\" name=\"Y\"". $count ."\" size=\"20\">";
$count++;
}
?>


This post has been edited by pr4y: 25 November 2008 - 07:16 PM

Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: PHP Query While Loop Help

Posted 26 November 2008 - 03:43 AM

Don't you mean while($count < $numrows)? Having it greater than will make it never return true.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5821
  • View blog
  • Posts: 12,674
  • Joined: 16-October 07

Re: PHP Query While Loop Help

Posted 26 November 2008 - 05:17 AM

You probably don't want to key off of count, just because it's hard to map back to what you're looking for. If you have a unique id, use that.

Maybe something like:
<? 
$query = "SELECT * from products WHERE `this` = 'that'";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
	$id = $row['product_id'];
	echo "<input type=\"text\" name=\"X$id\" size=\"20\"";
	echo ' value="' . $row['x'] . '"'
	echo '/>';
	echo "<input type=\"text\" name=\"Y$id\" size=\"20\"";
	echo ' value="' . $row['y'] . '"'
	echo '/>';
}
mysql_free_result($result);
?>


Was This Post Helpful? 0
  • +
  • -

#6 cookied89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 12-October 08

Re: PHP Query While Loop Help

Posted 27 November 2008 - 07:46 PM

Thanks, these posts were helpful.

Now.
One more thing, is there a way to get these in a table.

Like:

RESULT 1 | RESULT 2 | RESULT 3 |
RESULT 4 | RESULT 5 | RESULT 6

I've tried a few and all I seem to end up getting is:
RESULT1
RESULT2
RESULT3

Or

RESULT1|RESULT2|RESULT3| -----------> END QUERY.

Hmm.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1