Jump to content

[SOLVED] stripping last char problem


dennismonsewicz

Recommended Posts

Here's the code:

 

foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name = $key . ",";
		  $data_value .= $val;
		  
		  $length = strlen($data_name);
		  $fname = trim(substr($data_name,$length-1,$length));
		  $fname = str_replace($fname, " ", $data_name);
		  echo $fname;

	}

 

It is taking out every single one of the commas from the $data_name var and I need it to only take out the last comma..

 

Any ideas?

Link to comment
Share on other sites

Use strrchr to find the last occurence and just remove that from the string with substr_replace.

 

<?php
$data_name = substr_replace($data_name, "", strrchr($data_name, ","));
?>

 

Unless strrchr returns false... equating to 0.

 

Your best bet is

rtrim( $str, ',' );

Link to comment
Share on other sites

ok so here is the latest problem i am having

 

updated code:

 

<?php

ini_set ("display_errors", "1");
error_reporting(E_ALL);

include "includes/sql.php";

$action = $_GET['action'];

switch($action) {
case "foreach":
echo '<form action="foreach.php?action=results" method="post">';
		echo '<div><input type="checkbox" name="checkboxes[pm1]" class="checkbox" id="pm1" value="1" /> <label for="pm1">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer1]" class="checkbox" id="designer1" value="1" /> <label for="designer1">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm2]" class="checkbox" id="pm2" value="1" /> <label for="pm2">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[writer]" class="checkbox" id="writer" value="1" /> <label for="writer">Writer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm3]" class="checkbox" id="pm3" value="1" /> <label for="pm3">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer2]" class="checkbox" id="designer2" value="1" /> <label for="designer2">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm4]" class="checkbox" id="pm4" value="1" /> <label for="pm4">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[christine]" class="checkbox" id="christine" value="1" /> <label for="christine">Christine</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm5]" class="checkbox" id="pm5" value="1" /> <label for="pm5">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer3]" class="checkbox" id="designer3" value="1" /> <label for="designer3">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm6]" class="checkbox" id="pm6" value="1" /> <label for="pm6">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[julie]" class="checkbox" id="julie" value="1" /> <label for="julie">Julie</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm7]" class="checkbox" id="pm7" value="1" /> <label for="pm7">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer4]" class="checkbox" id="designer4" value="1" /> <label for="designer4">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm8]" class="checkbox" id="pm8" value="1" /> <label for="pm8">PM</label></div>';
echo '<div><input type="checkbox" name="checkboxes[proofreading]" class="checkbox" id="proofreading" value="1" /> <label for="proofreading">Proofreading</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm9]" class="checkbox" id="pm9" value="1" /> <label for="pm9">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[layne]" class="checkbox" id="layne" value="1" /> <label for="layne">Layne</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm10]" class="checkbox" id="pm10" value="1" /> <label for="pm10">PM</label></div>';
echo '<div><input type="checkbox" name="checkboxes[programming]" class="checkbox" id="programming" value="1" /> <label for="programming">Programming</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm11]" class="checkbox" id="pm11" value="1" /> <label for="pm11">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[testing]" class="checkbox" id="testing" value="1" /> <label for="testing">Testing</label></div>';
	echo '<input type="submit" value="Submit" />';
echo '</form>';

break;

case "results":
	foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name = $key . ",";
		  $data_value = $val;
		  
		  $data_name = rtrim($data_name, ',');
		  echo $data_name;

	}

	/*$qry = mysql_query("INSERT INTO fake (" . $data_name . ") VALUES ('$data_value')") or die(mysql_error());*/

break;

}

?>

 

the problem is, is that your code is treating each $key as an individual answer and so it stripping each comma occurance.

 

Any other ideas?

 

 

Link to comment
Share on other sites

Still the same problem :(

 

updated code:

 

<?php

ini_set ("display_errors", "1");
error_reporting(E_ALL);

include "includes/sql.php";

$action = $_GET['action'];

switch($action) {
case "foreach":
echo '<form action="foreach.php?action=results" method="post">';
		echo '<div><input type="checkbox" name="checkboxes[pm1]" class="checkbox" id="pm1" value="1" /> <label for="pm1">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer1]" class="checkbox" id="designer1" value="1" /> <label for="designer1">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm2]" class="checkbox" id="pm2" value="1" /> <label for="pm2">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[writer]" class="checkbox" id="writer" value="1" /> <label for="writer">Writer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm3]" class="checkbox" id="pm3" value="1" /> <label for="pm3">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer2]" class="checkbox" id="designer2" value="1" /> <label for="designer2">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm4]" class="checkbox" id="pm4" value="1" /> <label for="pm4">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[christine]" class="checkbox" id="christine" value="1" /> <label for="christine">Christine</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm5]" class="checkbox" id="pm5" value="1" /> <label for="pm5">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer3]" class="checkbox" id="designer3" value="1" /> <label for="designer3">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm6]" class="checkbox" id="pm6" value="1" /> <label for="pm6">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[julie]" class="checkbox" id="julie" value="1" /> <label for="julie">Julie</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm7]" class="checkbox" id="pm7" value="1" /> <label for="pm7">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer4]" class="checkbox" id="designer4" value="1" /> <label for="designer4">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm8]" class="checkbox" id="pm8" value="1" /> <label for="pm8">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[proofreading]" class="checkbox" id="proofreading" value="1" /> <label for="proofreading">Proofreading</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm9]" class="checkbox" id="pm9" value="1" /> <label for="pm9">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[layne]" class="checkbox" id="layne" value="1" /> <label for="layne">Layne</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm10]" class="checkbox" id="pm10" value="1" /> <label for="pm10">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[programming]" class="checkbox" id="programming" value="1" /> <label for="programming">Programming</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm11]" class="checkbox" id="pm11" value="1" /> <label for="pm11">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[testing]" class="checkbox" id="testing" value="1" /> <label for="testing">Testing</label></div>';
	echo '<input type="submit" value="Submit" />';
echo '</form>';

break;

case "results":
	foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name = $key . ",";
		  $data_value = $val;
		  
		  $pos = strrpos($data_name, ",");
		  echo substr_replace($data_name, '',$pos, 1);

	}

	/*$qry = mysql_query("INSERT INTO fake (" . $data_name . ") VALUES ('$data_value')") or die(mysql_error());*/

break;

}

?>

Link to comment
Share on other sites

now its deleting everything in the string accept for the last string

 

For Example:

 

echo '<form action="foreach.php?action=results" method="post">';
		echo '<div><input type="checkbox" name="checkboxes[pm1]" class="checkbox" id="pm1" value="1" /> <label for="pm1">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer1]" class="checkbox" id="designer1" value="1" /> <label for="designer1">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm2]" class="checkbox" id="pm2" value="1" /> <label for="pm2">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[writer]" class="checkbox" id="writer" value="1" /> <label for="writer">Writer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm3]" class="checkbox" id="pm3" value="1" /> <label for="pm3">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer2]" class="checkbox" id="designer2" value="1" /> <label for="designer2">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm4]" class="checkbox" id="pm4" value="1" /> <label for="pm4">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[christine]" class="checkbox" id="christine" value="1" /> <label for="christine">Christine</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm5]" class="checkbox" id="pm5" value="1" /> <label for="pm5">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer3]" class="checkbox" id="designer3" value="1" /> <label for="designer3">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm6]" class="checkbox" id="pm6" value="1" /> <label for="pm6">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[julie]" class="checkbox" id="julie" value="1" /> <label for="julie">Julie</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm7]" class="checkbox" id="pm7" value="1" /> <label for="pm7">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[designer4]" class="checkbox" id="designer4" value="1" /> <label for="designer4">Designer</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm8]" class="checkbox" id="pm8" value="1" /> <label for="pm8">PM</label></div>';
echo '<div><input type="checkbox" name="checkboxes[proofreading]" class="checkbox" id="proofreading" value="1" /> <label for="proofreading">Proofreading</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm9]" class="checkbox" id="pm9" value="1" /> <label for="pm9">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[layne]" class="checkbox" id="layne" value="1" /> <label for="layne">Layne</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm10]" class="checkbox" id="pm10" value="1" /> <label for="pm10">PM</label></div>';
echo '<div><input type="checkbox" name="checkboxes[programming]" class="checkbox" id="programming" value="1" /> <label for="programming">Programming</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[pm11]" class="checkbox" id="pm11" value="1" /> <label for="pm11">PM</label></div>';
		echo '<div><input type="checkbox" name="checkboxes[testing]" class="checkbox" id="testing" value="1" /> <label for="testing">Testing</label></div>';
	echo '<input type="submit" value="Submit" />';
echo '</form>';

 

If you run the above code then select two or more boxes:

 

foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name = $key . ",";
		  $data_value = $val;

	}

	echo trim($data_name, ',');

 

It will delete everyone of the answers in the checkboxes array except for the last one checked.

 

So if you check box1, box2, box3 the foreach code will spit out only box3

Link to comment
Share on other sites

the code you provided worked!

 

Here's a new problem lol

 

foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name .= $key . ",";
		  $data_value .= $val . ",";

	}

	$trimmed_name = trim($data_name, ',');
	$trimmed_value = trim($data_value, ',');

	$qry = mysql_query("INSERT INTO fake ($trimmed_name) VALUES ('$trimmed_value')") or die(mysql_error());

 

I am receiving the following mysql error:

 

Column count doesn't match value count at row 1

 

any ideas?

Link to comment
Share on other sites

try

<?php
$data_name = '`'.implode('`,`',array_flip($_POST['checkboxes'])).'`';
$data_value = "'".implode("','",$_POST['checkboxes'])."'";
$qry = mysql_query("INSERT INTO fake ($data_name) VALUES ($data_value)") or die(mysql_error());
?>

Link to comment
Share on other sites

The DB is setup with id being auto incremented and the primary key then the column names are the same names that match the checkbox names in the array. Each table cell holds a 0 until the information is passed to the DB that changes the 0 to a 1, essentially turning it "on".

 

sasa I tried your code and it only uses the last value in the checkbox array, it deletes the rest.

Link to comment
Share on other sites

I was doing some debugging and found out what my problem was

 

Code:

 

<?php
foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name .= $key . ",";
		  $data_value .= $val . ",";

	}

	$trimmed_name = trim($data_name, ',');
	$trimmed_value = trim($data_value, ',');

	$qry = "INSERT INTO fake ($trimmed_name) VALUES ('$trimmed_value')";

	echo $qry;

	/*$result = mysql_query($qry) or die(mysql_error());*/
?>

 

Is spitting out:

 

INSERT INTO fake (pm1,designer1) VALUES ('1,1')  

 

So its not treating the ones as individual answers but as one... anyway to fix this?

Link to comment
Share on other sites

problem solved!

 

Updated Code for anyone wanting to know how this works:

 

foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name .= $key . ",";
		  $data_value .= "'" . $val . "'" . ",";

	}

	$trimmed_name = trim($data_name, ',');
	$trimmed_value = trim($data_value, ',');

	$qry = mysql_query("INSERT INTO fake ($trimmed_name) VALUES ($trimmed_value)")or die(mysql_error());

Link to comment
Share on other sites

I would do this instead:

 

foreach($_POST['checkboxes'] as $key=>$val) 
	{
		  $data_name .= $key . ",";
		  $data_value .= "'" . $val . "'" . ",";

	}

	$trimmed_name = trim($data_name, ',');
	$trimmed_value = trim($data_value, ',');

	$qry = mysql_query(sprintf("INSERT INTO fake ('%s') VALUES ('%s')",
			mysql_real_escape_string($trimmed_name),
			mysql_real_escape_string($trimmed_value)))or die(mysql_error());

Link to comment
Share on other sites

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.