Old 05-05-2007, 00:06   #1 (permalink)
SpooF
Registered User
 
SpooF's Avatar
 
Join Date: Feb 2007
Posts: 31
Strange Code

Okay, lets see how non complicated I can make this sound. This relates to another type that I asked here.

I would link you but it wouldnt let me, also I couldnt link my pictures, sorry if they are small

Below is a function that I use to edit information in my database. $id is the primary key of the row in the database I want to edit. $data is the html that is used to display things properly on page (template for the editHack page). How I have this system setup is so that someone can post a "hack", aka tutorial on my webpage. This would be rather simple but I have made it so they can upload pictures specific to this hack. Now when they go to edit the hack I want all the pictures that are related to the hack to be placed back into a table so I can use AJAX to extract it and put them into a drop down menu (I use AJAX so they don't have to refresh the page when they upload an image, it simples just updates the drop down menu with the new image).

Now on to what a major part of this code does. Like the thread I referenced above, I am using a system that will insert variables into a html template. Thats for the most part what this code does. It takes the html template searches it for a specific pattern, and then inserts information where the pattern matches.

So my problem isn't so much the functioning of this script. It works fine for the most part. My problem comes when I have a specific set of code in my html template. If I have textarea tags ANYWHERE in the html template I encounter an "error" of sorts.

Ill give you a scenario:

I have the hack with the primary key ID that is equal to 1. In my hacknmod_pictures template it looks like this:

files.e-marvels.net/gif/table1.gif

pictureHID = The hack that the picture relates to.

So if I run my script and I have a html textarea tags in it I will get something like this in my hacknmod_temppictures

files.e-marvels.net/gif/table2.gif


As you can see it seems to be that the function has executed twice, or something similar. I have check everything I can think of. I have counted the number of times the function goes through a loop and the number of the times the function is executed in a session. If I have the html textarea tags I get the "double" post in the table shown above. The while loop runs two loops and the function exicuted once. If I remove any html textarea tags from the html template I get the results I want. It will not "double" post.

Does anyone have any idea why this is happening?

PHP FUNCTION:
PHP Code:
function editHack($id,$data) {
             
$query mysql_query("SELECT * FROM hacknmod_pictures WHERE pictureHID = '$id'");
             while (
false !== ($row mysql_fetch_assoc($query))) {
                @
mysql_query("INSERT INTO hacknmod_temp_pictures SET temp_pictureUSER = '".$_SESSION['id']."', temp_pictureNAME = '".$row['pictureNAME']."', temp_pictureURL = '".$row['pictureURL']."'");                    
            }
            
             
$myquery mysql_query("SELECT * FROM hacknmod_hacks WHERE hackID = '$id'");
             
$myrow mysql_fetch_array($myquery);
             
                
preg_match_all("/\[[A-Za-z0-9_\-]*?\]/",$data,$matches2);
                
$i=0;
                while(
$stop  !=  1)  {
                    if(
$matches2[0][$i])  {
                            
$matches[$i]  =  $matches2[0][$i];
                    }  else  {
                            
$stop  =  1;   
                    }
                    
$i++;
                }
        
                
$content  =  array();
                       
                foreach(
$matches  as  $value)  {
                            
                    
$first  =  explode("[",$value);
                    
$second  =  explode("]",$first[1]);
                     if(
$second[0] == "hackCATA") {
                        
$query = @mysql_query("SELECT * FROM hacknmod_catagory");
                        
$insert "";
                        while(
$row mysql_fetch_array($query)) {
                            if(
$myrow['hackCATA'] == $row['catagoryID']) {
                                
$insert.=  "<option value='".$row['catagoryID']."' selected>".$row['catagoryNAME']."</option>\n";
                            } else {
                                
$insert.=  "<option value='".$row['catagoryID']."'>".$row['catagoryNAME']."</option>\n";
                            }
                        }
                    } elseif(
$second[0] == "hackDIFF") {
                        
$query = @mysql_query("SELECT * FROM hacknmod_difficulty");
                        
$insert "";
                        while(
$row mysql_fetch_array($query)) {
                             if(
$myrow['hackDIFF'] == $row['difficultyID']) {
                                
$insert.=  "<option value='".$row['difficultyID']."' selected>".$row['difficultyNAME']."</option>\n";
                            } else {
                                
$insert.=  "<option value='".$row['difficultyID']."'>".$row['difficultyNAME']."</option>\n";
                            }
                        
                        }
                    } else {
                        
$insert $myrow[$second[0]];
                    }
                    
array_push($content,$insert);
                }
                     
                foreach(
$content  as  $value)  {
                    
$data  =  preg_replace('/\[[A-Za-z0-9_\-]*?\]/',  $value,  $data,1); 
                }
                
             
                echo 
$data;

        } 

HTML TEMPLATE:
HTML Code:
<script language="javascript" type="text/javascript"> <!-- function changePic(img_name) { box = document.imageSelection.picSelect; destination = box.options[box.selectedIndex].value; document[img_name].src=destination; } function addPic() { box = document.imageSelection.picSelect; destination = box.options[box.selectedIndex].value; document.main.hackBODY.value += "[img=" + destination + "]"; } function AddStandard(img_name) { box = document.imageSelection.picSelect; destination = box.options[box.selectedIndex].value; document.main.hackPIC.value = destination; } //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data sent from the server ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var message = ajaxRequest.responseText; document.getElementById('loading').innerHTML = ajaxRequest.responseText; document.imageSelection.Update.value = "Update"; changePic('myPic'); } else { document.imageSelection.Update.value = "Loading..."; } } ajaxRequest.open("GET", "?module=HackNMod&act=viewImagesTemp&notemp=1", true); ajaxRequest.send(null); } var Type = 'Z'; var set = 0; function StartFloat() { if(set == '1') { if(document.all) { document.all.imgFORM.style.display = 'none'; } else if(document.layers) { document.imgFORM.display = 'none'; } else if(document.getElementById) { document.getElementById('imgFORM').style.display = 'none'; } set = '0'; document.main.upload.value = "Show"; } else { document.main.upload.value = "Hide"; if(document.all) { //document.all.AdFloater.style.pixelLeft = document.body.clientWidth - document.all.AdFloater.offsetWidth; document.all.imgFORM.style.display = 'block'; set = '1' Type = 'A'; } else if(document.layers) { //document.AdFloater.left = window.innerWidth - document.AdFloater.clip.width - 16; document.imgFORM.display = 'block'; set = '1' Type = 'B'; } else if(document.getElementById) { //document.getElementById('AdFloater').style.left = (window.innerWidth - 35) + 'px'; document.getElementById('imgFORM').style.display = 'block'; set = '1' Type = 'C'; } if (document.all) { window.onscroll = Float; } else { setInterval('Float()', 100); } } function Float() { if (Type == 'A') { document.all.imgFORM.style.pixelTop = document.body.scrollTop; } else if (Type == 'B') { document.imgFORM.top = window.pageYOffset; } else if (Type == 'C') { document.getElementById('imgFORM').style.top = window.pageYOffset + 'px'; } } } //--> </script> <table> <tr> <td colspan='2'> <iframe name='imgFORM' width='100%' height='120' src='?module=HackNMod&act=uploadImage&temp=2' style='display = none'> </iframe> </td> </tr> <tr> <td valign='top'> <form name='imageSelection'> <table> <tr> <td> <input type='button' value='Update' name='Update' onClick="ajaxFunction();" /> </td> <td> <div id='loading'> <select> </select> </div> </td> </tr> </table> <table> <tr> <td colspan='2'> <img name='myPic' src='images/noimage.gif' width='150' height='150' onClick="addPic();" /> </td> </tr> <tr> </table> </form> </td> <td valign='top'> <form name='main' action='?module=HackNMod&act=edit_Hack&id=[hackID]' method='post'> <table> <tr> <td> Title: </td> <td> <input type='text' value='[hackTITLE]' name='hackTITLE' /> </td> </tr> <tr> <td> Catagory: </td> <td> <select name='hackCATA'> [hackCATA] </select> </td> </tr> <tr> <td> Difficluty: </td> <td> <select name='hackDIFF'> [hackDIFF] </select> </td> </tr> <tr> <td> Digg: </td> <td> <input type='text' value='[hackDIGG]' name='hackDIGG' /> </td> </tr> <tr> <td> Stander Image: </td> <td> <input type='button' value='Add Current' name='Standard' onClick='AddStandard();'> <input type='text' value='[hackPIC]' name='hackPIC' /> </td> </tr> <tr> <td> Upload Image: </td> <td align='center'> <input type='button' value='Show' name='upload' onClick='StartFloat()');> </td> </tr> </table> </td> </tr> <tr> <td colspan='2'> <table> <tr> <td valign='top'> Tags:<br> <font size='small'>seperate by semi colons ';'</font> </td> <td> <textarea name='hackTAGS'>[hackTAGS]</textarea> </td> </tr> <tr> <td valign='top'> Short Description: </td> <td> <textarea name='hackSHORT'>[hackSHORT]</textarea> </td> </tr> <tr> <td valign='top'> Body: </td> <td> <textarea name='hackBODY' id='large'>[hackBODY]</textarea> </td> </tr> </table> </td> </tr> <tr> <td> <input type='submit' value='submit' \> </td> </tr> </form> </table>
  Reply With Quote
Old 05-05-2007, 01:16   #2 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
COMMENTS

PHP Code:
// COMMENTS

/* COMMENTS */

<!-- COMMENTS --> 

Why does you regexp pattern match and empty set of square brackets? and the question mark at the end was/is not necessary. the question mark makes the * ungreedy so that the pattern would not match the ending bracket but your patterned can't match and end brackets so it's just wasting space.
Code:
/\[[A-Za-z0-9_\-]*?\]/
should probably be
Code:
/\[[A-Za-z0-9_\-]+\]/

also if you're using regexp you shulding need to do this...
PHP Code:
$first  =  explode("[",$value);
$second  =  explode("]",$first[1]); 

try this pattern and print out the array you'll see the contents of your square brackets are already on their own. Notice the round brackets.
Code:
/\[([A-Za-z0-9_\-]+)\]/

PHP: Pattern Syntax - Manual


Maybe if you comment your code we wold be more willing to help... I'm too lazy to run through your code line by line trying to figure out exactly whats happening.
  Reply With Quote
Old 05-05-2007, 03:14   #3 (permalink)
SpooF
Registered User
 
SpooF's Avatar
 
Join Date: Feb 2007
Posts: 31
Well Ill give you a simpler function that does the exact same thing.

PHP Code:
function editHack($id,$data) {
    
# Moves a set of rows to a new table in the database for reason of keeping things 
    # orgranized in the process of editing
             
$query mysql_query("SELECT * FROM hacknmod_pictures WHERE pictureHID = '$id'");
             while (
false !== ($row mysql_fetch_assoc($query))) {
                @
mysql_query("INSERT INTO hacknmod_temp_pictures SET temp_pictureUSER = '".$_SESSION['id']."', temp_pictureNAME = '".$row['pictureNAME']."', temp_pictureURL = '".$row['pictureURL']."'");                    
            }
            
             
                echo 
"<textarea></textarea>";
        } 

Basicly it doesnt matter what I put in that function, if I send a html textarea tags to the browser its as if the function executes twice.

Quote:
Why does you regexp pattern match and empty set of square brackets?

Because thats what Im looking for. I look through the code to find a set of square brackets. When I do I take whats inside of it and process that though my script to insert information in place of it.

Quote:
I am using a system that will insert variables into a html template. Thats for the most part what this code does. It takes the html template searches it for a specific pattern, and then inserts information where the pattern matches.

So my problem isn't so much the functioning of this script. It works fine for the most part. My problem comes when I have a specific set of code in my html template. If I have textarea tags ANYWHERE in the html template I encounter an "error" of sorts.
  Reply With Quote
Old 05-05-2007, 03:23   #4 (permalink)
gk
geek
 
gk's Avatar
 
Join Date: Oct 2006
Location: *.everywhere
Posts: 204
Send a message via ICQ to gk Send a message via AIM to gk Send a message via MSN to gk Send a message via Yahoo to gk
Quote:
Originally Posted by SpooF
Basicly it doesnt matter what I put in that function, if I send a html textarea tags to the browser its as if the function executes twice.

Then it's likely the problem is not in the function itself but in w/e code calls the function.
__________________
Quote:
"Why reinvent the wheel"? Simply because it's not round enough and I don't like the treads.
  Reply With Quote
Old 05-05-2007, 13:33   #5 (permalink)
SpooF
Registered User
 
SpooF's Avatar
 
Join Date: Feb 2007
Posts: 31
Well I removed the textarea from within the function and move it out right before the function is called. Tested it and it still gave me the problem. I then move the textarea to the very first one line of my index page (Out side of any php tags) and I still get the problem. So from what I can see, if there is a textarea on the page it will screw up.

So I dont think this is a problem with my coding anymore. Its just a strange set of code that makes no sense to me at all. I cant imagine how it could possible screw something up like that. Im going to some more test to see if any other query's mess up.


I have other function on my webpage that run same generic html template The only difference is I insert more information into the edit hack template than something like the add hack template. The addHack function works perfectly except if I have add the html textarea anywhere, such as the very first line.

Last edited by SpooF : 05-05-2007 at 13:47.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8