Converting an array into a string in Ajax

Source: Internet
Author: User

Main Page;

  

Processing page:

<?phpinclude (".. /dbda.class.php "); $db =new Dbda (); $sql =" SELECT * from Info ";//Method One: Normal method Write directly://$attr = $db->query ($sql);//returns an array, To convert to a string//because you want to return a string as a table, you need to split the string//split into this form: "p001^ Zhang San |p002^ John Doe |p003^ Harry";/* $str = ""; foreach ($attr as $v) {$str =$ Str.implode ("^", $v); Stitch each column of data into a string $str= $str. "|"; /stitching per line}*///sunstr: Intercepts a string, gets the string from the nth position in the string s, starting with the length Len,//$str =substr ($str, 0,strlen ($STR)-1);//This step is to remove the end of the "|" Symbol//echo $STR;//Method Two: Use Dbda in the method of encapsulation echo $db->strquery ($sql);

  

The running results show:

Attachment 1: Converting an array of Ajax into a string wrapper into a class
The AJAX call returns the string public function strquery ($sql, $type =1, $db = "MyDB") {//Build Connection object $conn = new Mysqli ($this->host, $this- UID, $this->password, $db);//Determine if the connection is successful!mysqli_connect_error () or Die ("Connection failed! ");//Execute SQL statement $result = $conn->query ($sql);//Determine SQL statement type if ($type ==1) {$attr = $result->fetch_all (); $str =" ";// If the query statement returns a string for ($i =0; $i <count ($attr), $i + +) {for ($j =0; $j <count ($attr [$i]); $j + +) {$str = $str. $attr [$i] [$j];$ str = $str. " ^";} $str = substr ($str, 0,strlen ($STR)-1); $str = $str. "|";} $str = substr ($str, 0,strlen ($STR)-1); return $str;} else{//if it is a different statement, return TRUE or Falseif ($result) {return "OK";} Else{return "NO";}}}

  

Attached 2: In the Statistics function (count), it is convenient to use strquery ($sql) to return the string directly

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

The returned result is 1.

Converting an array into a string in Ajax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.