serialize function is not working in php - php

Hi i get the value from post i used serialize function its showing wrong value. Its just displaying N.. pls help me
Php Code
$item_select_alpha = $_POST['item_select_alpha'];
for ($alpha = 1; $alpha <= count($item_select_alpha); $alpha++) {
$serialise = serialize(array($item_select_alpha[$alpha]));
}
$item_quanity = $_POST['item_quanity'];
for ($qty = 1; $qty <= sizeof($item_quanity); $qty++) {
$item_quan = serialize($item_quanity[$qty]);
}
print_r($item_quan);
exit;
HTML Code
<select class="item_select_alpha" name="item_select_alpha[]">
<option value="">select the Alphabetic</option>
{foreach $size_alpha as $sa}
<option value="{$sa['size_id']}">{$sa['size_name']}</option>
{/foreach}
</select>
<input type="text" class="item_quanity" name="item_quanity[]" class="form-control">

You not add items to an array, but only changes the variable so it contains the last element. Try this:
$serialise[] = serialize(array($item_select_alpha[$alpha]));
$item_quan[] = serialize($item_quanity[$qty]);

your variables in for loops will contains just the last element, you update their value at each iteration. $item_quan defined in the second loop won't be "printable" out of the loop...

Related

PHP show blank value in dropdown list that's fed NULL value from database

I've got a setup for showing years in my webform that updates each year that's working good.
Since I need to do math on the year value selected by the user, I've made it an INT in the MySQL database. Making the value INT returns a zero to the select field where ever the user hasn't put any information in. The user will leave some of these fields empty.
Here's the code I'm currently using to populate the year select box:
<div class="kstyle">
<label for="c02">C #02 </label>
<select id="c02" name="c02">
<option value=""></option>
<option value="<?=$row['c02'];?>" selected><?=$row['c02'];?></option>
<script>
var min = new Date().getFullYear() - 45;
cur = new Date().getFullYear();
select = document.getElementById('c02');
for (var i = min; i<=cur; i++){
var opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
</script>
</select>
</div>
This returns a zero in any INT field that doesn't have a value selected.
The issue I'm running into is an aesthetic one. I'd like the page to show a completely blank result whenever the database returns zero/NULL.
I'm working on tweaking the above code with an if - else statement but keep running into syntax errors associated with equal signs, left and/or right carrot signs. This tells me that these values are being evaluated as arithmetic, which means I probably need to tweak where I put parenthesis and/or quotes. This however is outside the bounds of what I've read in the PHP manual, so I need some help.
Here's where I'm at right now with the code:
<div class="kstyle">
<label for="c03">C #03 </label>
<select id="c03" name="c03">
<option value="<?php if($row['c03']=NULL)
{ echo '';
} else {
<$row['c03']; selected><$row['c03'];}>?>"></option>
<script>
var min = new Date().getFullYear() - 45;
cur = new Date().getFullYear();
select = document.getElementById('c03');
for (var i = min; i<=cur; i++){
var opt = document.createElement('option');
opt.value = i;
opt.text = i;
select.appendChild(opt);
}
</script>
</select>
</div>
If anybody could show me where I'm screwing up or a different approach to this, I'd be eternally grateful.
Unless I'm not quite understanding what you're trying to do, it just looks like your output syntax is wrong.
Try this?
<option value="<?php
if($row['c03'] == null){
echo '">';
} else {
echo $row['c03'] . '" selected >';
}
?>
</option>
You could check for 0 and null with:
<?= !empty($row['c02']) ? $row['c02'] : null ?>
But, why not generate all the options with PHP:
$minY = date('Y',strtotime('-45 years')); // ## 45 Years Ago
$curY = date('Y'); // ## This Year
$selY = !empty($row['c03']) ? $row['c03'] : null; // ## Resultset Year
echo "<select id=\"c03\" name=\"c03\">\n";
echo "\t<option value=\"\">Select a Year</option>\n"; // ## Blank/Prompt Option
for($i = $minY; $i <= $curY; $i++) {
$selected = ($i == $selY) ? 'selected' : null;
echo "\t<option $selected value=\"$i\">$i</option>\n";
}
echo "</select>\n";
The "\n" (newline) and "\t"(tab) are not necessary but result in more legible code. This should give you something like:
<select id="c03" name="c03">
<option value="">Select a Year</option>
<option value="1972">1972</option>
<option value="1973">1973</option>
...
<option value="2017">2017</option>
</select>
First ting first, your if condition is wrong. Compare it with ==
if($row['c03']==NULL)
Second,You say you have 0 value in the database and if condition is checking NULL, which I think is wrong. It should be
if($row['c03']==0)
And there is syntax error for option tag. It should be like this
<option value="<?php if($row['c03']==0)
{ echo '';
} else {
$row['c03'];?> selected><?php echo $row['c03'];}?>"></option>

Using Variable Inside $_POST[ ]

How to use variable inside $_POST as i have used the following code but it says undefined offset in second line. How can i solve it?
$p = $_GET['cii'];
$selectOption = $_POST[$p];
Below is the code:
echo'<form method ="POST">
<select name="'.$abc[3].'">
<option value="slow">slow</option>
<option value="medium">medium</option>
<option value="fast">fast</option>
</select>
<br>
Click to change
</form>';
$abc[] has some numbers and the select box is made with same name as the number.
Almost there:
$p = 'cii';
$selectOption = $_POST[$p];
you are accessing same page when you come first time you get undefined offset
So first check existence
if(isset($_GET['cii'])){
$p = $_GET['cii'];
$selectOption = $_POST[$p];
}
Also no need to add double && just use single & here
Click to change
Assuming variable $abc[3] gives you right value.

Retrieve name ID from php json file

I have this php file for country/state/town list using json method.
PHP file:
sleep(1);
$stateID = $_GET['stateID'];
$countyID = $_GET['countyID'];
$townID = $_GET['townID'];
$html = $_GET['html'];
$states = array();
$states['MA'] = "Massachusetts";
$states['VT'] = "Vermont";
$states['SC'] = "South Carolina";
$counties = array();
$counties['MA']['BARN'] = 'Barnstable';
$counties['MA']['PLYM'] = 'Plymouth';
$counties['VT']['CHIT'] = 'Chittenden';
$counties['SC']['ANDE'] = 'Anderson';
$towns = array();
$towns['MA']['BARN']['CHA'] = "Chatham";
$towns['MA']['BARN']['DEN'] = "Dennis";
$towns['MA']['BARN']['YAR'] = "Yarmouth";
$towns['MA']['PLYM']['BRI'] = "Bridgewater";
$towns['MA']['PLYM']['MAR'] = "Marshfield";
$towns['MA']['PLYM']['WAR'] = "Wareham";
$towns['VT']['CHIT']['BUR'] = "Burlington";
$towns['VT']['CHIT']['ESS'] = "Essex";
if($stateID && !$countyID && !$townID){
echo json_encode( $counties[$stateID] );
} elseif( $stateID && $countyID && !$townID ) {
echo json_encode( $towns[$stateID][$countyID] );
} elseif( isset($villages[$stateID][$countyID][$townID]) ) {
echo json_encode( $villages[$stateID][$countyID][$townID] );
} else {
echo '{}';
}
I retrieve value from MySql database(example:MA for country).
Now I need to print country/state/town name into select box dropdown like this.
<select id="country" class="validate[required]" name="country">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$country['selector'].'">Show Country Name From php File</option>
</select>
<select id="state" class="validate[required]" name="state">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$state['selector'].'">Show state Name From php File</option>
</select>
<select id="town" class="validate[required]" name="town">
<option value="0">Choose ...</option>
<option selected="selected" value="'.$town['selector'].'">Show town Name From php File</option>
</select>
How can I print this?
Create a php file that makes the call to the database. The results are, normally, stored in a php array. You can turn this array into Json using json_encode()(http://www.php.net/manual/en/function.json-encode.php).
Simply echo this in the php-file.
Now call that script with jQuery. There is a simple jQuery method called getJson(). - Read more about it here: http://api.jquery.com/jquery.getjson/
This will bring the MySQL results to you - in Json form - and you could now use JavaScript to loop the results, and display the form accordingly. A simple example would be (I haven't tested this but I hope you get the idea):
$.each(data, function() {
$('#mySelect')
.append($("<option></option>")
.attr("value",data[row].NameOfColumn)
.text(value));
row ++;
});
Note the name of column - that's the name of the key for the value you are looking for. In your case you might want to loop the keys as well - with an outer loop - since the data seems to be presented in such a matter.

Breaking from foreach loop

I'm working on a website wherein rows are generated depending on how many users there are. In this example, I have three users. Basically, I pass data through $_POST using drop down select data. Here's what I'm passing to PHP. These are wrapped in <form> but I cleaned it to show just the important data.
...
<select name="taction[3]" >
<option value="accept">Accept</option>
<select name="taction[4]" >
<option value="accept">Accept</option>
<select name="taction[6]" >
<option value="accept">Accept</option>
...
My php looks like this:
$total = 1;
foreach ($_POST['taction'] as $userid => $action)
{
if ($action == "accept")
{
if ($total<1)
{
break;
}
else
{
echo $userid."foo";
$total = ($total - 1);
}
}
}
For some reason, it is still displaying three "foo's" when it should've stopped after the first "foo". What am I doing wrong?
Change it to
if($total <= 1)
Or start the $total variable at 0.
Thanks for your suggestions. I was at fault by relying on variables outside the foreach statement. I had to copy the operations inside the loop again for it to register the added data.

post values from a multiselect list to a mysql query using php and jquery

Hi guys I am trying to post values which is getting number from another text box for MySQL select query but i am stuck can u please help me here is my code when I try to get result I cannot add comma(,) between values. also tried implode() and explode() function but the result only got number of array element please help me. I will be glad to try your ideas thanks.
on my sql query i get only row as a result which is my first select
thanks a lot for your help again guys
function exportselectionlist(){
var qcolumns=document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
<form id="selectionlist_form" action="xxx.php" method="post"
name="selectionlist_form">
<select id="selectionlist" style="width:300px;" multiple="multiple" size="4"
name="selectionlist[]">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
<input type="submit" value="x" />
<a onclick="exportselectionlist()" href="javascript:;">Export</a>
</form>
//xxx.php
<?php foreach ($selectionlist as $value) {
$resultstr = array();
foreach ($selectionlist as $result)
$resultstr[] = $result;
echo $x=implode(",",$resultstr);
sql = mysql_query("SELECT * FROM table where idArticle in ('$x')");
Try changing your js function to:
function exportselectionlist() {
var qcolumns = document.getElementById('selectionlist');
for (i=0; i < qcolumns.length; i++) {
qcolumns.options[i].selected = true;
}
document.selectionlist_form.submit();
}
and then your "xxx.php" to:
$selectionlist = $_POST['selectionlist'];
echo implode(',', $selectionlist);
As a side note, your php code indicates to me that you have register_globals turned on? I would recommend turning that off in favor of creating the variable you need from the $_POST superglobal.

Categories