简体   繁体   中英

How to loop the $row = mysql_fetch_array($rs);

I have a table 在此处输入图片说明

Now.i have a function in my JS

function add()


{      
                <?php 
include('conn.php');
$rs = mysql_query("select * from position");
$row = mysql_fetch_array($rs);
$ss=$row['Name']; 
$sss=$row['nowb'];
$ssss=$row['totalb'];
$sssss=$row['nowc'];
$ssssss=$row['totalc'];
echo "add2()";
?>}

function add2(){
         AddAddress("<?php echo $ss;?>","<?php echo $sss;?>/<?php echo $ssss;?><br /><?php echo $sssss;?>/<?php echo $ssssss;?>");
         }

How to get the every date from my table?

Something like this?

function add() {      
<?php 
include('conn.php');
$rs = mysql_query("select * from position");
while ( $row = mysql_fetch_array($rs) ) {
  $ss=$row['Name']; 
  $sss=$row['nowb'];
  $ssss=$row['totalb'];
  $sssss=$row['nowc'];
  $ssssss=$row['totalc'];
  echo 'AddAddress("' . $ss . '","' . $sss . '/' . $ssss . '<br />' . $sssss . '/' . $ssssss . '");';
}
?>
}

Didn't text the echo 'AddAddress....' line so I hop eI got all the single and double quotes in the right place??

Performing POST requests using Ajax here is an example of sending data from js to php.

also stop naming your vars s,ss,sss,ssss you will have no idea what they mean when you read your code tomorrow..

and try not to use mysql_* functions they have been deprecated switch to mysqli or pdo

I got what would you like to do. In your PHP file:

function add(){
<?php
include('conn.php');
$rs = mysql_query("select * from position");
echo "var data = [] ; "
while($row = mysql_fetch_assoc($rs)){
  echo "
    data.push({
      name:   '{$row['Name']}',
      nowb:   '{$row['nowb']}',
      totalb: '{$row['totalb']}',
      nowc:   '{$row['nowc']}',
      totalc: '{$row['totalc']}'
    }); \n\r " ;
}
?>
add2(data);
}

function add2(data){
  for (var i in data){
    var row = data[i] ;
    AddAddress(row.name, row.nowb, row.totalb, row.nowc, row.totalc);
  }
}

If I understand the question correctly you want to know how to loop through an array in php.

$row = mysql_fetch_array($rs);
foreach($row as $value){
    //Do something
}

Read up on it in the docs http://php.net/manual/en/control-structures.foreach.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM