0

Hello my problem is simple , i have a table news , i have a button bt1 , and a div to show the results , i want to display all rows from table when i click the button,i only get the first row , how can i display all results ?

    <script>
function shownews(id) { <? php
    include('db.php');
    $query = mysql_query("SELECT * FROM news");
    while ($row = mysql_fetch_array($query)) {
        $a = $row['news_title'];
    } ?>
    var ab = <? php echo json_encode($a); ?> ;
    id.innerHTML = ab;
}
</script>
<div id="results"></div>
<button id="btn1" onclick="shownews(results)">See news</button>
1
  • 2
    You should use mysqli, mysql is deprecated. Commented Feb 7, 2014 at 11:30

5 Answers 5

1

try

$a = array();
while ($row = mysql_fetch_array($query)) {
            $a[] = $row['news_title'];
        } ?>
// print array

print_r($a);
Sign up to request clarification or add additional context in comments.

1 Comment

$a = array(); is not necessary actually - ...the $var[] behaviour where a new array is created. (us2.php.net/array_push)
1

Your echo json_encode($a); is not in your while loop, so you only render 1 line. Also if i understand what you're doing, you want your PHP to be executed only when you trigger your button click ? This is not the way to do it... php is a server language where javascript is executed only by your browser.

I didn't

Try this :

<script type="text/javascript">
    function shownews(id) {
        document.getElementById(id).innerHTML = document.getElementById('news').innerHTML ;
    }
</script>
<div id="news" style="display:none ;">
<?php
    include ('db.php');
    $query=mysql_query("SELECT * FROM news");
    while($row=mysql_fetch_array($query)){
     echo $row['news_title'] . '<br />' ;
    }
?>
</div>
<div id="results"></div>
<button id="btn1" onclick="shownews('results')">See news</button>

Comments

0

You are overwriting $a, hence you will only get the last row.

Change

while($row=mysql_fetch_array($query)){
    $a=$row['news_title'];
}

to

$a = array();
while($row=mysql_fetch_array($query)){
    array_push($a, $row['news_title']);
}

Edit: Royal_bg is correct - extra info:

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function. https://www.php.net/array_push

Comments

0

You execute all the while before writing anything. Try to change the closing bracket after writing the answer...

<script>

  function shownews(id){

<?php

    include ('db.php');
    $query=mysql_query("SELECT * FROM news");

    while($row=mysql_fetch_array($query)){
      $a=$row['news_title'];

?>

      var ab = <?php echo $a; ?>;
      id.innerHTML += ab;

<?php

    }

?>

  }
</script>

<div id="results"></div>
<button id="btn1" onclick="shownews(results)">See news</button>

1 Comment

Two things: json_encode is not necessary since $a is just a string. And this doesn't solve the problem since id.innerHTML=ab will overwrite what's in the div anyway.
0
  function shownews(id){



  <?php
  include ('db.php');
  $query=mysql_query("SELECT * FROM news");

  while($row=mysql_fetch_array($query)){
  $a[]=$row['news_title'];

  }



   ?>


  id.innerHTML=<?php echo json_encode($a); ?>;

  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.