开发者

php syntax query

开发者 https://www.devze.com 2022-12-14 23:28 出处:网络
i have 2 questions 开发者_JS百科to ask: first of all, in while ($row = mysql_fetch_object($result))

i have 2 questions 开发者_JS百科to ask: first of all, in while ($row = mysql_fetch_object($result))

i have an object: $row->atitle

now through javascript, i want to print on the page an ++increment of the current object of the array.

i am doing this:

hidephp = document.write("<?php echo .$row->atitle.["+inc+"];"+" ?>");

the above doesnt seem to work. help would be much appreciated.


You've offered so little to work with, that I'm really not sure of where to begin. I assume that from your post, you're not using any kind of templating process (you should really look into that)

Your problem is really that you're mixing languages that span the client/server relationship. Javascript can do stuff with what the server sends the client, while PHP actually decides what to send in the first place. If you want Javascript to do something with variables in PHP, you should look into using PHP's json_encode() to do what you want:

// PHP
$row = array()
while ($row = mysql_fetch_object($result)
{
    $data[] = $row;
}
$javascriptFriendlyData = json_encode($data);


// Javascript
data = <?= $javascriptFriendlyData ?>;


You can't print out PHP like that. PHP runs when a page is requested, it outputs HTML and JavaScript to the browser, then the browser shows the HTML and JavaScript. By the time the JavaScript is run, PHP is done.


As it is, you are trying to interpret the PHP part from client side, which is not possible.
To be interpreted at server side, you should write something similar to

idephp = document.write("<?php echo $row->atitle; ?>");

In this case, PHP will execute the code inside the <?php ?> tag.


Maybe I'ven't understand your needs but you don't need javascript to write the title to the result html document.

Try simply.

while ($row = mysql_fetch_object($result))
  echo $row->atitle;

these two lines will consume all the recordset fetched from the query

If you need to embed a dinamically created javascript into an html page served via php you should try this php code:

while ($row = mysql_fetch_object($result))
  echo "\nhidephp = document.write(\"" .$row->atitle."\");";

If you need to dinamically update a page already served to the browser you will need Ajax (and I'm not giving you a complex solution without further details).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号