trying to reduce script size

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

trying to reduce script size

Post by IGGt »

I currently have a php script set up that executes 25 MySQL queries, creates 25 variables, and then places these into a html table.

They are along the lines of:

Code: Select all

$result1 = mysql_query("SELECT colour from db1.squares WHERE row=1 and `column`=1")
$result2 = mysql_query("SELECT colour from db1.squares WHERE row=1 and `column`=2")

$row1 = mysql_fetch_array( $result1 );
$row2 = mysql_fetch_array( $result2 );

<td bgcolor=<?php echo $row1['colour']?>>&nbsp;</td>
<td bgcolor=<?php echo $row2['colour']?>>&nbsp;</td>

So I have $result1, $result2, $result3 . . . and so on up to $result25 (and the same for the $row)


In SQL I could simply use the query:

SELECT colour from db1.squares

and this would give me all the results I need in one go.

Is there a way I could use this query, and simply call the results one after the other, thus much reducing the size of my php script? I'm still new to PHP, so any help would be appreciated.
Last edited by Benjamin on Wed May 19, 2010 4:11 am, edited 1 time in total.
Reason: Added [syntax=php] tags.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: trying to reduce script size

Post by mikosiko »

One option:

Code: Select all

$result = mysql_query("SELECT colour from db1.squares");

while ($myrow = mysql_fetch_assoc($result))               // or mysql_fetch_array if you prefer
{
   <td bgcolor=<?php echo $myrow['colour']?>>&nbsp;</td>
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: trying to reduce script size

Post by AbraCadaver »

mikosiko is on the right track, but you probably want to order the results and then start a new table row when the row changes. Something like:

Code: Select all

$result = mysql_query("SELECT `colour` FROM `db1`.`squares` ORDER BY `row`, `column` ASC");
$prev_row = 1;

echo '<tr>';
while($row = mysql_fetch_array($result)) {
	if($row['row'] != $prev_row) {
		echo '</tr><tr>';
	}
	echo '<td bgcolor=' . $row['colour'] . '&nbsp;</td>';
}
echo '</tr>';
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: trying to reduce script size

Post by IGGt »

cheers, that seems to be nearly there. The squares are appearing (albeit in a single column, not in a 5x5 square - but I can probably tidy that up later) with the correct colours in them.

The only thing is each square now says

"Notice: Undefined index: row in C:\wamp\www\squares5.php on line 23"

The code I have put in is:

Code: Select all

$result = mysql_query("SELECT `colour` FROM `db1`.`squares` ORDER BY `row`, `column` ASC");
$prev_row = 1;

echo '<table cellspacing="3" cellpadding="10" height="300px" width="300px">';
echo '<tr>';
while($row = mysql_fetch_array($result)) {
        if($row['row'] != $prev_row) {
                echo '</tr><tr>';
        }
        echo '<td bgcolor=' . $row['colour'] . '&nbsp;</td>';
}
echo '</tr>';
echo '</table>';
?>
Do I need to declare row as a variable or something?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: trying to reduce script size

Post by AbraCadaver »

My bad. I just copy and pasted your query. You need to select the `row` column also:

Code: Select all

$result = mysql_query("SELECT `colour`, `row` FROM `db1`.`squares` ORDER BY `row`, `column` ASC");
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: trying to reduce script size

Post by IGGt »

Ah, that makes sense.

cheers
Post Reply