Jump to content


Photo

Funky rtrim() quark


  • Please log in to reply
4 replies to this topic

#1 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 13 March 2006 - 04:08 AM

Any explanations?

<?php

$query_assm = "INSERT INTO shops ('name', 'location', 'manager', 'phone',";

$query_assm = rtrim($query_assm, ",");

echo $query_assm;

//Still echos the trailing comma

?>

Am I running into a regex type of thing here?

Do I need a ' \ ' deliminater?

I am pulling my friggin' hair out and I am running out of Jolt!
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 March 2006 - 04:22 AM

I just tried your code. It works fine for me.

How did you generate the string? I assume it's generated, since you wouldn't just create one with a trailing comma.

Ken

#3 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 13 March 2006 - 04:43 AM

Here is the whole method, minus the method name:

------
P.S the "$get[]" is the $_GET array but I use a data cleaning method that puts $_GET into $get
------

if ($get[0]['maintenancechg']) {
    
    $post = $_POST;
    
    array_pop($post);
    
    $assm = "INSERT INTO " . $get[1]['type'] . " (";
    
    foreach ($post as $key => $value) {
        
        $assm .= "'$key', ";
    
    }
    
    $assm = rtrim($assm, ",");
    
    $assm .= ") VALUES (";
    
    foreach ($post as $key => $value) {
        
        $assm .= "'$value', ";
    
    }
    
    $assm = rtrim($assm, ",");
    
    $assm .= ")";
    
    echo $assm;

    die();

    $sql_manager->__Construct("breakdown");

AAAAAAAAAAHHHHHHHHHHHHHHHH

it's the FREAKING space char " , " ISNOT "," AAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

I can't rtrim "," when ", " is what is at the end

Thanks for the help bro
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 March 2006 - 04:50 AM

That's what I thought you were doing...

Instead of
<?php
    $assm = "INSERT INTO " . $get[1]['type'] . " (";
    foreach ($post as $key => $value) {
        $assm .= "'$key', ";
    }
    $assm = rtrim($assm, ",");
    $assm .= ") VALUES (";
    foreach ($post as $key => $value) {
        $assm .= "'$value', ";
    }
    $assm = rtrim($assm, ",");
    $assm .= ")";
    echo $assm;
?>
use a temporary array and the implode() function:
<?php
    $assm = "INSERT INTO " . $get[1]['type'];
    $tmp = array()
    foreach ($post as $key => $value) {
        $tmp[] = $key;
    }
    $assm .= "(`" . implode("`,`",$tmp) . "`) VALUES ";
    $tmp = array();
    foreach ($post as $key => $value) {
         $tmp[] = mysql_real_escape_string($value);
    }
    $assm .= "('" . implode("','",$tmp) . "');
    echo $assm;
?>
Or you can use the "set field = 'value'" variant:
<?php
    $assm = "INSERT INTO " . $get[1]['type'] . " SET ";
    $tmp = array()
    foreach ($post as $key => $value) {
         $tmp[] = "`" . $key . "` = '" . mysql_real_escape_string($value) . "'";
    }
    $assm .= implode(',',$tmp);
    echo $assm;
?>

I usually use something similar to the latter example.

Ken

#5 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 13 March 2006 - 05:01 AM

I like your way better because it's not a "Kentucky Hack", like my way. Your way leaves no doubt where my way is sort of like "damage control".

-Thanks
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users