简体   繁体   中英

How to go to an end of recordset using php?

Here is the code:

<?php
  require'connection.php';

  $cons = mysql_query( 'SELECT Subject FROM tblsubject' );

  $total_rows = mysql_num_rows($cons);
  $counter = 0;
  $fields="";
  while($row = mysql_fetch_array($cons)) {
    if(++$counter == $total_rows) {
$fields .= $row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width:     \'20% \' <br/>}<br/>';
  }
  else {
$fields =  $row['Subject'].': { <br/> title: \''.$row["Subject"].'\',<br/> width:    \'20% \' <br/>},<br/>';
   }
  }

  echo $fields;
?>

The code stated above should produce an output like this below:

General: {
  title:'General',
  width: '20%',
},
Math: {
  title:'Math',
  width: '20%',
}

But if the query reaches its last row, the "," should be omitted after the last "}". I'm getting it right but when I echo it outside the while loop, only the two last row on the database are echoed.

Can someone help me with this? Please tell me where I am going wrong. Thank you very much :DI need to echo all the data in the table not just the final two.

Don't even bother with a complicated state checker. Just build an array and then implode it. Automatic "proper" number of commas.

while($row = mysql_fetch_array()) {
    $array[]= $row['data_you_want'];
}
echo implode(',', $array);

You could try something like this:

$print = '';
while($row = mysql_fetch_array($cons)) {
    $row_subject_array[] = $row['Subject'];
}

for($i = 0; $i < count($row_subject_array); $i++) {
    $print .= $row_subject_array[$i]. ': { <br/> title: \''.$row_subject_array[$i].'\',<br/> width:     \'20% \' <br/>},';
}
$print = rtrim($print, ',');

echo "<br />";

Also a little suggestion: You should stop of use mysql_x() functions. These functions are deprecated, instead of it you can use mysqli_x() function or PDO.

Why shouldn't I use mysql_* functions in PHP?

Hello and Welcome to Stack Overflow!

First of all you should avoid using the mysql class and use mysqli as mysql has been deprecated.

Now for your question, If your MySQL table has an ID column with Auto Increment enabled then you can do this:

SELECT id FROM table ORDER BY id DESC LIMIT 1

However if you want to achieve the same result then I would format my code in the following way:

$cons = mysql_query( 'SELECT Subject FROM tblsubject' );

$fieldsArray = array();

while($row = mysql_fetch_array($cons)) {
    array_push($fields_array,$row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width:     \'20% \' <br/>},<br/>');
}

array_pop($fieldsArray);

array_push($fieldsArray,$row['Subject']. ': { <br/> title: \''.$row['Subject'].'\',<br/> width:     \'20% \' <br/>}<br/>');

foreach($fieldsArray as $value) {
    echo $value;
}

tada!

i always do:

 $var = substr($var,0,-1);

 echo $var; //or whatever

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