Jump to content

Reducing Code Size


Bendude14

Recommended Posts

I have this code and i am sure there must be away to make it smaller. The other problem im having i think may also be fixed at the same time. Let me sure you the code to explain further

 

<?php 

while($dbarray = mysql_fetch_row($result)) {


echo "<input type=\"text\" name=\"".$dbarray['1']."\" value=\"".$dbarray['1']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['2']."\" value=\"".$dbarray['2']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['3']."\" value=\"".$dbarray['3']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['4']."\" value=\"".$dbarray['4']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['5']."\" value=\"".$dbarray['5']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['6']."\" value=\"".$dbarray['6']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['7']."\" value=\"".$dbarray['7']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['8']."\" value=\"".$dbarray['8']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['9']."\" value=\"".$dbarray['9']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['10']."\" value=\"".$dbarray['10']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['11']."\" value=\"".$dbarray['11']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['12']."\" value=\"".$dbarray['12']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['13']."\" value=\"".$dbarray['13']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['14']."\" value=\"".$dbarray['14']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['15']."\" value=\"".$dbarray['15']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['16']."\" value=\"".$dbarray['16']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['17']."\" value=\"".$dbarray['17']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['18']."\" value=\"".$dbarray['18']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['19']."\" value=\"".$dbarray['19']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['20']."\" value=\"".$dbarray['20']."\" /><br />";
echo "<input type=\"text\" name=\"".$dbarray['21']."\" value=\"".$dbarray['21']."\" /><br />";



}

?>

 

Is there a way i can just you var $i instead of having to hard code all the different rows i am retrieving? Also sometimes only 15 rows exists in the DB so then i get undefined variables. Is there a way to display only rows that exist?

 

Thanks for the Help

Link to comment
https://forums.phpfreaks.com/topic/123552-reducing-code-size/
Share on other sites

Thanks thats exactly what i was looking for.

 

it seems to be causing this error though now

 

Notice: Undefined offset: 22 in C:\wamp\www\mqserver\admin\gallery_captions.php on line 51

 

Line 51 is the first line of the code you gave me.

 

Thanks again

Link to comment
https://forums.phpfreaks.com/topic/123552-reducing-code-size/#findComment-638106
Share on other sites

What it does is go through the the subscripts until it hits one that it undefined, when then evaluates to false, when then breaks the loop.

 

So, yes, you are using an undefined offset... that is the point, and also why I code with notices turned off.

 

Add error_reporting(2). Or turn it off in WAMP... however you do that...

Link to comment
https://forums.phpfreaks.com/topic/123552-reducing-code-size/#findComment-638110
Share on other sites

Or you could make the code just a little more verbose and include some proper error checking as well as add clarity. [Notice the use of isset()]

 

while($dbarray = mysql_fetch_row($result))
{
    for($t=1; isset($dbarray[$t]); $t++)
    {
        echo "<input type=\"text\" name=\"".$dbarray[$t]."\" value=\"".$dbarray[$t]."\" /><br />";
    }
}

Link to comment
https://forums.phpfreaks.com/topic/123552-reducing-code-size/#findComment-638121
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.