0

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 :D I need to echo all the data in the table not just the final two.

ankur140290
  • 640
  • 9
  • 25
Nixxhalle
  • 97
  • 3
  • 5
  • 10

4 Answers4

1

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);
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • wow! this is great it real does solve my problem .. Thank you very much for this, it really solved my problem.. I'm just a beginner in php and still not familiar with all its functions .. thank you again :D – Nixxhalle Feb 05 '14 at 03:46
  • By the way, can use this if i'm going to put this on the query for example: the normal query is like this: msqyli_query(Science,Math,general)values('sometext',etc).. can i use the $array like this: msqyli_query( " ',',$array?? " )values('sometext',etc).. thanks :D – Nixxhalle Feb 05 '14 at 04:05
0

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?

Community
  • 1
  • 1
Drakantas
  • 11
  • 2
0

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!

Hurricane Development
  • 2,449
  • 1
  • 19
  • 40
0

i always do:

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

 echo $var; //or whatever
bart2puck
  • 2,432
  • 3
  • 27
  • 53