Jump to content

[SOLVED] i am having a comma issue ...


imarockstar

Recommended Posts

I have a script that echos out sample code .. and it works fine .. but when it prints each block of code out ... there is a comma at the end of each array .. i know why it there .. but i dont know a trick to get it off there ..

 

here is an example ...

 

if there are 5 form fields, each asking for a variable name and the user enters this :

 

name

email

phone

city

state

 

then the array is :  name,email,city,state,

 

you can see that there is a COMMA after state, and you can see below that I am putting it there so the code output it valid. However i dont need the code on the last post entry. is there a way i can take that out ?

 

here is my code  :

 

$tablename = $_POST['tablename'];
$header = $_POST['header'];

if ( isset($_POST['v']) ) { 
    $vary = implode(',', $_POST['v']); }   
    else { $vary = ""; } ?>
    
    <code clss="getcode">
            <p>
			<?php
			$a = explode(',', $vary);
		    foreach ($a as $v) { 
		    echo "$".$v."=\$_POST['" . $v . "']; <br>";
		    }
			?>
			</p> 
	</code>

    <h4 class="codehead">Insert Code</h4>
    <code clss="getcode">
	        <p>
		    $sql="INSERT INTO <?php echo $_POST['tablename'];?> ( 
		    <?php
			$a = explode(',', $vary);
		    foreach ($a as $v) { 
		    echo "".$v.",";
		    }
			?> ) <br>

		    VALUES ( 
		    <?php
			$a = explode(',', $vary);
		    foreach ($a as $v) { 
		    echo "'".$v."',";
		    }
			?> );
			</p> 
	</code>  


	<h4 class="codehead">Update Code</h4>
    <code clss="getcode">
	        <p>
		    mysql_query("UPDATE merch SET 
		    <?php
			$a = explode(',', $vary);
		    foreach ($a as $v) { 
		    echo "".$v."='".$v."',";
		    }
			?> ");

			</p> 
	</code> 







                
        <code clss="getcode">
                <p>
		    $result = mysql_query($sql) or die("ERROR: " . mysql_error() . "SQL: " . $sql); <br>

	        header("Location: <?php echo $_POST['header'];?>");
	        </p> <br>
	</code>

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/177437-solved-i-am-having-a-comma-issue/
Share on other sites

I would do it like this so that you aren't making a comparison at every iteration:

             <?php
            $string = "";
            $a = explode(',', $vary);
             foreach ($a as $v) { 
             $string .= "'".$v."',";
             }
             echo substr_replace($string, null, -1);
            ?>

 

Though I don't understand why you are taking an array, turning it into a string, turning it back into an array and then, finally, turning it back into a string again.

Archived

This topic is now archived and is closed to further replies.

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