Jump to content

PHP toggle code similar to javascript toggle div script


djinnov8

Recommended Posts

Hi everyone...

 

I recently came across some jquery/js code that enables you to (toggle) showand hide a section of html code contained within a div...

 

Now I am trying to do the same thing but with the results of a mysql query contained within PHP...

 

so looking at sample code...

$query = "SELECT first_name, last_name, comments from members"

$result=mysql_query($query);

 

if(mysql_num_rows($result) < 0)

{

    echo "There are no members";

}

else

{

while($rows = mysql_fetch_array($result))

    {

          echo $rows['first_name'] . '<br />';

          echo $rows['last_name'] . '<br />';

          echo $rows['comments'] . '<br />';

    }

}

 

How can I (toggle) show and hide the comments row for each individual member...

 

Thanks for your reply in advance

 

 

Link to comment
Share on other sites

Php is a server side scripting language. It does not have the ability to do anything in the browser. It simply outputs content (HTML, Javascript, css, media files) to the browser when the browser makes http request to the web server.

 

Perhaps if you define exactly what you mean by -

 

How can I (toggle) show and hide the comments row for each individual member...
Link to comment
Share on other sites

Thanks for the reply...yes - as I mentioned in the post...show and hide the comments of each member similar to the javascript/jquery function....

I've seen this done on countless sites including (to some extent) Facebook...and Im guessing this is the clever use of javascript/jquery and html within the php code...

 

Php is a server side scripting language. It does not have the ability to do anything in the browser. It simply outputs content (HTML, Javascript, css, media files) to the browser when the browser makes http request to the web server.

 

Perhaps if you define exactly what you mean by -

 

How can I (toggle) show and hide the comments row for each individual member...

Link to comment
Share on other sites

Maybe I should've labelled this PHP toggle functionality similar to js toggle script

 

(I've heard that you can break out of php midway through a statement or you could enclose key parts of the javascript script within (brackets) and echo other parts to get it to work within PHP code...

 

This wouldn't work but I hope it lets you know what I'm after...

 

<?php...

{

while($rows = mysql_fetch_array($result))

    {

          echo $rows['first_name'] . '<br />';

          echo $rows['last_name'] . '<br />';

          ?>

            <script language="javascript"> function toggle() {

    var ele = document.getElementById("toggleText");

    var text = document.getElementById("displayText");

    if(ele.style.display == "block") {

    ele.style.display = "none";

text.innerHTML = "show";

      }

      else {

      ele.style.display = "block";

      text.innerHTML = "hide";

            }

              }

              </script>

            <?php

          echo '<div id='toggleText' style='display: none'>';

          echo '<h1>' . $rows['comments'] . '</h1></div>';

          echo '<a id='displayText' href='javascript:toggle();'>show</a> <== click Here';

         

          echo '</div>';

    }

}

...?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.