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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.