Jump to content

Recommended Posts

Hi,

   I have a little snipplet I am posting here, I am not very experienced with PHP but thought I would ask some of you for help.  I have to modify a program and a very small part of it is to remove lines where values are being set to blank or '' null values. Here is the little code snipplet:

 

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";

 

This above code is used to set some values to ' ', but then after I would like those blank values removed rather then showing up in the list because when I scroll down you have a whole bunch of blank lines. How can I remove these completely?

Maybe:

echo "\t\t\if(document.pickDivision.cloth.options[index].text = '') {...some code here to remove this blank line}

I read about regex or some str_replace, but am not sure how to use it.  This program is around 3000 lines of code, so don't think I can attach the file.  Any help would be greatly appreciated!

 


echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t\if(document.pickDivision.cloth.options[index].text != '')\n";   // <-------------- add
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";

what do you mean by "showing up in the list" ?

 

It looks like you are using php to echo out some javascript to execute. Well you can't make php turn around and remove the code - it's already been sent to the browser. And even then you can't make javascript remove lines of code in the source. IOW "no takebacks".

what do you mean by "showing up in the list" ?

 

It looks like you are using php to echo out some javascript to execute. Well you can't make php turn around and remove the code - it's already been sent to the browser. And even then you can't make javascript remove lines of code in the source. IOW "no takebacks".

You are right Josh, the things is that it's a little weird the way this program was made, initially some values show up which need to be set to blank so that they don't show for  certain options chosen by the user.  So in the beginning I guess everyone was okay to see blank lines because in theory they don't have any value except null I guess, now they want those blank lines to be removed.  I read that preg_replace or str_replace can help?

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t\if(document.pickDivision.cloth.options[index].text != '')\n";   // <-------------- add
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";

Thanks Guru but it didn't work, existing code has it set to blank first and everyone wants those blanks to be removed.  You can see my explanation in the above post if it makes sense.

Okay well can you please define what "blank lines" means to you?

 

Are you talking about removing the php lines of code that set javascript variables to empty string values?

 

If so, then just open up the php file and remove them...now you see it:

 

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";
now you don't.

 

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t}\n\n";
OOR...are you saying this is exactly what you want to do, but you have like 3000 lines of code to go through, and want a simple way to find and remove them from the php source file? If this is the case, then I would advise against making it completely automatic. There's no guarantee that they are all in the format you listed above, and it sure would suck if you were to end up removing more than you intend..

 

Instead, I would open the file in an editor that has a regex find and replace feature (that prompts you before actually replacing).

 

But a regex like this should find the ones you listed above:

 

echo\s*".*?=\s*''

I think they are talking about removing blank entries from an HTML dropdown.  That JS is doing something to set the entries to '' so they appear as blank in the dropdown.

 

Is this correct?

 

 

Not tested:

echo "\t\t\tdocument.pickDivision.cloth.remove(index);\n";
Edited by AbraCadaver

 

I think they are talking about removing blank entries from an HTML dropdown.  That JS is doing something to set the entries to '' so they appear as blank in the dropdown.

 

Is this correct?

 

 

Not tested:

echo "\t\t\tdocument.pickDivision.cloth.remove(index);\n";

Actually yes, that's it, they want to remove the blank lines that show up in the HTML dropdown.  The JS is indeed setting the entries coming from a database table to ' ', so they are appearing as blanks.  I tried to put your line above after the

line: echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";

But it didn't work.  I will try again now.

Okay well can you please define what "blank lines" means to you?

 

Are you talking about removing the php lines of code that set javascript variables to empty string values?

 

If so, then just open up the php file and remove them...now you see it:

 

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";
now you don't.

 

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t}\n\n";
OOR...are you saying this is exactly what you want to do, but you have like 3000 lines of code to go through, and want a simple way to find and remove them from the php source file? If this is the case, then I would advise against making it completely automatic. There's no guarantee that they are all in the format you listed above, and it sure would suck if you were to end up removing more than you intend..

 

Instead, I would open the file in an editor that has a regex find and replace feature (that prompts you before actually replacing).

 

But a regex like this should find the ones you listed above:

 

echo\s*".*?=\s*''

 

Sorry Josh again for not being too clear, what I mentioned to Cracka Memba above is what I want to do.

Edited by reptile

Try checking fo null instead of empty string

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t\if(document.pickDivision.cloth.options[index].text != null)\n";   // <-------------- change
echo "\t\t{\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].text = '';\n";
echo "\t\t\tdocument.pickDivision.cloth.options[index].value = '';\n";
echo "\t\t}\n\n";

Forget that last feeble effort of mine, try this one instead

echo "\t\tfor(index=0; index < $maxclothrows; index++)\n";
echo "\t\t{\n";
echo "\t\t\tif (document.pickDivision.cloth.options[index].text == '')\n";
echo "\t\t\t\tdocument.pickDivision.cloth.options[index]=null;\n";
echo "\t\t}\n\n";

Alternatively, if the options are coming from a database table, change the query so those with blank values are exluded.

Thanks for your help thus far, that would be extremely easy to exclude nulls, the problem is this.  The dropdown list is dependent on an option selection.

2 options for example, A and B.  If the user selects option A then lets say there are a max# of cloths that are brought back at 20.  Now when the user selects option B then max# of cloths brought back are 15.  So this is coded in a way that because option A has 5 more values then option B, to clear those extra values of option A away when option B is selected, those extra 5 values are nulled out or set to ' '.  This is the point where I would like to remove those extra 5 blank lines from the dropdown.  Hope that made sense?

sorry to jump in here, but your approach is backwards. you are creating $maxclothrows empty entries at one point, apparently putting some values in an another point, and wanting to remove the remaining empty entires after the point where you have put in the actual data.

 

the correct approach would be to initialize/create just the entries you need at the point where the actual values are being retrieved at. why can you not do this directly? and if you did have a need to first create $maxclothrows empty entries, the place in the code where you would test for and remove the remaining empty ones would be right after the point where you have put in the actual data.

 

so, what is your code that is putting in the actual data values?

sorry to jump in here, but your approach is backwards. you are creating $maxclothrows empty entries at one point, apparently putting some values in an another point, and wanting to remove the remaining empty entires after the point where you have put in the actual data.

 

the correct approach would be to initialize/create just the entries you need at the point where the actual values are being retrieved at. why can you not do this directly? and if you did have a need to first create $maxclothrows empty entries, the place in the code where you would test for and remove the remaining empty ones would be right after the point where you have put in the actual data.

 

so, what is your code that is putting in the actual data values?

This is part of another while loop, but the basic code is here to print all the cloth selections dependent on the division (option the user can choose, there are 2 to choose from).

    // Print all cloth choices for the given division

    $cloth_query = "SELECT cloth,code FROM ".strtolower($division_row['CLOTHDB'])." ORDER BY CLOTH";

    $cloth_result = oci_parse($connect,$cloth_query);

    oci_execute($cloth_result);

    $cloth_numrows = count_rows($connect,$cloth_query);

    echo "\t\t\tdocument.pickDivision.cloth.options[0].value = '';\n";

    echo "\t\t\tdocument.pickDivision.cloth.options[0].text = '- Select Cloth -';\n\n";

    $y=1;

     while ($cloth_row = oci_fetch_array($cloth_result, OCI_ASSOC)) {

         $clothArray[] = "{$cloth_row['CLOTH']}";

        $cloth_array[$indexdiv][$indexcloth]="{$cloth_row['CLOTH']} {$cloth_row['CODE']}";    

      echo "\t\t\tdocument.pickDivision.cloth.options[$y].value = '{$cloth_row['CLOTH']} {$cloth_row['CODE']}';\n";

      echo "\t\t\tdocument.pickDivision.cloth.options[$y].text = '{$cloth_row['CLOTH']} {$cloth_row['CODE']}';\n\n";

      

      $y++; $indexcloth++;

    }

I did but still no luck, appreciate your help though so far.

I was sure that one (#11) would work.

 

I set up a small test page

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $( document ).ready(function() {
        removeBlanks();
 })
 

function removeBlanks()
 {
     var size = document.form1.sel1.options.length;
     for (var i=0; i<size; i++) {
         if (document.forms[0].sel1.options[i].text=='') {
             document.forms[0].sel1.options[i] = null;
         }
     }
 }
</script>
</head>
<body>
    <form name='form1'>
        <select name='sel1'>
            <option value='1'></option>
            <option value='2'>2</option>
            <option value='3'></option>
            <option value='4'>4</option>
        </select>
    </form>
</body>
</html>

Note options 1 and 3 have no text to appear in the dropdown.

 

After loading and the removeBlanks() function has run, Firebug shows this, with the blanks removed

<body>
<form name="form1">
<select name="sel1">
<option value="2">2</option>
<option value="4">4</option>
</select>
</form>
</body>

 

I was sure that one (#11) would work.

 

I set up a small test page

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
 $( document ).ready(function() {
        removeBlanks();
 })
 

function removeBlanks()
 {
     var size = document.form1.sel1.options.length;
     for (var i=0; i<size; i++) {
         if (document.forms[0].sel1.options[i].text=='') {
             document.forms[0].sel1.options[i] = null;
         }
     }
 }
</script>
</head>
<body>
    <form name='form1'>
        <select name='sel1'>
            <option value='1'></option>
            <option value='2'>2</option>
            <option value='3'></option>
            <option value='4'>4</option>
        </select>
    </form>
</body>
</html>

Note options 1 and 3 have no text to appear in the dropdown.

 

After loading and the removeBlanks() function has run, Firebug shows this, with the blanks removed

<body>
<form name="form1">
<select name="sel1">
<option value="2">2</option>
<option value="4">4</option>
</select>
</form>
</body>

Your logic seems correct for sure, unfortunately it's not working for me so there has to be more to it in the code, yours does work just not in my program because as Josh mentioned there seems to be more afterwards.  I've put code in my previous post where values are inserted from the database.

well what i meant by that was that the problem is somewhere else in your code - code you have not posted.  All of the help you've received has been based off what you've actually posted and explained.  So either you didn't apply the code to the right place, or maybe your code has several places it needs to be applied to, etc..

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.