13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Viewing the Tree of Articles<br />

751<br />

n<br />

n<br />

threads exp<strong>and</strong>ed.The exp<strong>and</strong>ed variable is an associative array that contains the<br />

postid of articles that will have their replies exp<strong>and</strong>ed.<br />

The parameter exp<strong>and</strong>, which tells the script which new threads to exp<strong>and</strong>.<br />

The parameter collapse, which tells the script which threads to collapse.<br />

When you click a plus or minus symbol or the Exp<strong>and</strong> or Collapse button, this action<br />

recalls the index.php script with new parameters for exp<strong>and</strong> or collapse.You use<br />

exp<strong>and</strong>ed from page to page to track which threads should be exp<strong>and</strong>ed in any given view.<br />

The index.php script begins by starting a session <strong>and</strong> adding the exp<strong>and</strong>ed variable<br />

as a session variable if this has not already been done. After that, the script checks<br />

whether it has been passed an exp<strong>and</strong> or collapse parameter <strong>and</strong> modifies the<br />

exp<strong>and</strong>ed array accordingly. Look at the code for the exp<strong>and</strong> parameter:<br />

if(isset($_GET['exp<strong>and</strong>'])) {<br />

if($_GET['exp<strong>and</strong>'] == 'all') {<br />

exp<strong>and</strong>_all($_SESSION['exp<strong>and</strong>ed']);<br />

} else {<br />

$_SESSION['exp<strong>and</strong>ed'][$_GET['exp<strong>and</strong>']] = true;<br />

}<br />

}<br />

If you click on the Exp<strong>and</strong> button, the function exp<strong>and</strong>_all() is called to add all the<br />

threads that have replies into the exp<strong>and</strong>ed array. (We look at this in a moment.)<br />

If you try to exp<strong>and</strong> a particular thread, you will be passed a postid via exp<strong>and</strong>.You<br />

therefore add a new entry to the exp<strong>and</strong>ed array to reflect this.<br />

The exp<strong>and</strong>_all() function is shown in Listing 31.3.<br />

Listing 31.3 exp<strong>and</strong>_all() Function from discussion_fns.php—Processes the<br />

$exp<strong>and</strong>ed Array to Exp<strong>and</strong> All the Threads in the Forum<br />

function exp<strong>and</strong>_all(&$exp<strong>and</strong>ed) {<br />

// mark all threads with children as to be shown exp<strong>and</strong>ed<br />

$conn = db_connect();<br />

$query = "select postid from header where children = 1";<br />

$result = $conn->query($query);<br />

$num = $result->num_rows;<br />

for($i = 0; $ifetch_row();<br />

$exp<strong>and</strong>ed[$this_row[0]]=true;<br />

}<br />

}<br />

This function runs a database query to work out which of the threads in the forum have<br />

replies, as follows:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!