Web Analytics Made Easy -
StatCounter attaining value of key value - CodingForum

Announcement

Collapse
No announcement yet.

attaining value of key value

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • attaining value of key value

    hi i am doing a delete structure for some records and im alittle confused here.

    so what i have is this

    this is the message array

    Code:
     [message] => Array
            (
                [0] => 603/msg_receiverdel
                [1] => 602/msg_receiverdel
                [2] => 597/msg_receiverdel
            )
    what im trying to do is split off the number and then loop thru and delete that rec id from the table.

    PHP Code:

    foreach ($message as $key=>$value) {
                    
    $arr_value=explode("/",$value);

       
    $query="DELETE FROM messages WHERE msg_id='??????'";

     } 
    and the question mark is where im having trouble. i dont know if i access the number itself like 603 by
    $arr_value[value];
    or
    $arr_value[key][value];
    or
    do i need to do a $i loop and use $arr_value[$i][value];

    thanks.
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  • #2
    $arr_value[0] should do it.
    "Tango says double quotes with a single ( ' ) quote in the middle"
    '$Name says single quotes with a double ( " ) quote in the middle'
    "Tango says double quotes ( \" ) must escape a double quote"
    '$Name single quotes ( \' ) must escape a single quote'

    Comment


    • #3
      thanks tango how do i scan thru them if i want to delete 603 602 597 will 0 do that for me.
      If a php file only has php code within it you do not need to use the closing php tag
      A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
      durangod is short for durango dave

      Comment


      • #4
        You will want to do something like this:
        PHP Code:
        foreach ($message as $key => $value
        {
            
        $myRecID = (int) $value;
            
        // or
            
        list($myRecID) = explode('/'$value);
            
            if(!empty(
        $myRecID) && is_numeric($myRecID)) // kinda optional
            
        {
                
        $query 'DELETE FROM `messages` WHERE `msg_id` = ' . (int) $myRecID
                
        // execute query
            
        }

        Comment


        • #5
          thanks to you both for that, ill have to tinker a bit because its not del them from the db, i checked the db and its the right info so ill have to tinker a bit. Thanks so much for the info really really appreciate it folks.
          If a php file only has php code within it you do not need to use the closing php tag
          A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
          durangod is short for durango dave

          Comment


          • #6
            Originally posted by durangod View Post
            thanks to you both for that, ill have to tinker a bit because its not del them from the db, i checked the db and its the right info so ill have to tinker a bit. Thanks so much for the info really really appreciate it folks.
            Are you actually running the $query with mysql_query() or similar?

            Comment


            • #7
              yes sir i am..


              PHP Code:

               
              foreach ( $message as $key=>$value) {

                             
              // $arr_value=explode("/",$value);
                             //$myRecID = (int) $value;
                            // or

                           
              list($myRecID) = explode('/'$value);
                  
                        if(!empty(
              $myRecID) && is_numeric($myRecID)) // kinda optional
                          
              {
                         
              $query='DELETE FROM `messages` WHERE `msg_id` = ' . (int) $myRecID
                          
              // execute query

                         
              $result=mysql_query($query,$link) or die(mysql_error());
                        }

              i might try changing this

              ' . (int) $myRecID;
              to
              '$myRecID';

              only because sometimes my server for some reason has a hard time with the (int) deal
              Last edited by durangod; Aug 17, 2011, 06:17 PM.
              If a php file only has php code within it you do not need to use the closing php tag
              A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
              durangod is short for durango dave

              Comment


              • #8
                Originally posted by durangod View Post
                i might try changing this

                ' . (int) $myRecID;
                to
                '$myRecID';

                only because sometimes my server for some reason has a hard time with the (int) deal
                That's not going to work, and I suspect you misinterpret the problem. I've never seen any server or version of PHP have a problem casting ints in that fashion.

                You may want to output the query instead of running it to see what is generated. Then you can run it in HeidiSQL or PHPMyAdmin to see if the rows get removed. You can also dump mysql_affected_rows() after you run the query to see if the rows were removed. You may be thinking there is an error when in fact the rows didn't exist or were referenced incorrectly. msg_id is the primary key in your table, correct? And it is an int?

                Comment


                • #9
                  thanks i found the issue, the issue was my filter i as using from the form input. When i changed the filter it began to work.

                  this was the filter it was on..



                  PHP Code:

                  function clearInput($value) {
                   
                  $find = array(
                      
                  '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
                      
                  '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
                      
                  '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
                      
                  '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
                  );
                   
                      
                  $result preg_replace($find''$value);
                      
                  $result mysql_real_escape_string($result);
                      return 
                  $result;
                  }

                  and 
                  this is the filter i change it to.. 

                  function 
                  form_get($value)
                    {  
                        
                  $get_value '';
                    
                          if (isset(
                  $_REQUEST[$value])) 
                            {
                            
                  $get_value=$_REQUEST[$value];
                            }

                          if (isset(
                  $_GET[$value])) 
                            {
                            
                  $get_value=$_GET[$value];
                            }

                        if (isset(
                  $_POST[$value])) 
                            {
                            
                  $get_value=$_POST[$value];
                            }          

                       if (
                  get_magic_quotes_gpc() && is_string($get_value))
                        {
                        
                  $get_value stripslashes($get_value);
                        }
                        
                  set_magic_quotes_runtime(0);
                        
                  $get_value is_string($get_value) ? mysql_real_escape_string($get_value) : $get_value;  

                      return 
                  $get_value
                  I think at one time i had changed the name of the filter but i did not change the filter name on this page.

                  Here is the php that i did get to work. Now that i know it was the filter i will post your code in there and im thinking it will work now. But i just wanted to share with you what worked is all.

                  PHP Code:
                  if(isset($delete) && isset($message)) 
                   { 
                  // the message variable is a list of msg_ids to delete from the email


                          
                  foreach ($message as $key=>$value)
                              {
                               list(
                  $arr_value) = explode("/",$value);        

                                                 
                               
                  $query="DELETE FROM messages WHERE msg_id='$arr_value'"
                               
                  $result=mysql_query($query,$link) or die(mysql_error());

                            }
                  //close foreach
                  }//close if 



                  As far as the (int) i do not know what the deal is with that, i know in the past i have had table fields that are assigned as lets say int 11 and then in the input in the php side i do have $var = (int)$numeric_var just to reassure myself that it is int but i have had times there for some reason that would not process correctly, dont ask me why i have no idea, but i know the form submitted an integer because i looked at it, but it would not process unless i removed the (int). So i actually got away from using it. I have never had an issue with is_numeric but i have with (int).
                  Last edited by durangod; Aug 18, 2011, 02:42 PM.
                  If a php file only has php code within it you do not need to use the closing php tag
                  A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
                  durangod is short for durango dave

                  Comment


                  • #10
                    If it is a numbered array, you can directly use array[0]

                    Comment

                    Working...
                    X