Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP - format posted array for MySQL statement

Posted on 2011-09-07
5
Medium Priority
?
189 Views
Last Modified: 2012-05-12
Hi,
is there a way to get just numerical data from a posted array and put into a format of ('n','n','n') so that I can use in a MySQL query

Examples of posted variable subcat below.

Array ( [0] => select_products [1] => 156 )  
Array ( [0] => select_products [1] => 118 [2] => 133 [3] => 134 )
Array ( [0] => 153 [1] => 151 [2] => 152 [3] => 158 [4] => 137 [5] => 138 [6] => 135 ) --->  
Array ( [0] => 174 ) --->  



I want to create something like this from the above
('156')
('118','133','134')
('153','151','158','137','138','135')
('174')


Thanks in advance for your feedback.
0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 15

Expert Comment

by:Insoftservice
ID: 36500894
Array ( [0] => select_products [1] => 156 )

if(is_array($array)){
      foreach($array as $key=>$value)
      {
            is_numeric($value)
            {
             $data[] = $value
            }
      }
}

echo '<pre>';print_r($data);
0
 
LVL 7

Expert Comment

by:boon86
ID: 36500918
try this:

<?php
$a=array("select_products ","156");
$b=array("select_products ","118", "133", "134");
$c=array("153","151","152", "158", "137","138","135");
$d=array("174");


echo "('$a[1]')<br />";
echo "('$b[1]','$b[2]','$b[3]')<br />";
echo "('$a[1]')<br />";
echo "('$c[0]','$c[1]','$c[2]','$c[3]','$c[4]','$c[5]','$c[6]')<br />";
echo "('$d[0]')<br />";
?>

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 2000 total points
ID: 36501150
if you want only integers:

<?php

$a = Array ( 0 => 'select_products', 1 => 118, 2 => 133, 3 => 134 ); 
$b = Array ( 0 => 153, 1 => 151, 2 => 152, 3 => 158, 4 => 137, 5 => 138, 6 => 135 );

$a_str = "(";

foreach($a as $val)
    if(is_int($val)) $a_str .= "'".$val."',";
$a_str = substr($a_str,0,-1);
$a_str .= ")";

echo $a_str;

echo"<br /><br />";

$b_str = "(";

foreach($b as $val)
    if(is_int($val)) $b_str .= "'".$val."',";
$b_str = substr($b_str,0,-1);
$b_str .= ")";

echo $b_str;
?>

Open in new window

0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36502101
You can use array_values() to remove the keys from the array.  You can use implode() to create a string from the array, using a comma as the "glue" string.  Then you can use the concatenation operator to wrap the string of numbers in parentheses.  This is a fairly common construct in PHP programming.  But I am curious... What do you mean by a "posted" array?  Where does this data originate?  Can you please show us the CREATE TABLE statement and show us the relationship between the external data and the columns you want to keep in the data base?  The data samples above do not give us much of a clue about how to create the query string that you might need.  Thanks, ~Ray
0
 

Author Closing Comment

by:sabecs
ID: 36502150
Thanks Roads_Roads, that's perfect.
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question