• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

Select form in php

I have created some code below which creates a select box / form with values from mysql.

This allows the user to assing an item to another item, in this case assign sub pages to top level pages.

For some reason it seems to be showing pages which are flagged as deleted deletestatus = ''

I have got it working on the "edit Item" code below, but not the "add item". any ideas?

Edit item shows the correct items in the select form.

Add item shows all pages, doesnt seem to filter the results.
Add item.
-----------------------------

<?php $get = "SELECT pageid, pagetitle, parentid FROM pages WHERE parentid='' or Parentid is null AND deletestatus = '' ORDER by pageorder";
		$answer = mysql_query($get)
    	or die ("Couldn't execute query."); 
		
		
		
		echo "<select name='parentid' class='gradientform'>\n";
		echo "<option value=''>Main Menu\n";
		//echo "<option value='0'>Seperate page\n";
		while ($row = mysql_fetch_array($answer))
  		{
    	extract($row);
	
		$current_parent_id = $row['parentid'];
		
    	echo "<option value='$pageid'>$pagetitle\n";

        $query = "SELECT pageid, pagetitle FROM pages WHERE parentid=" . $row['pageid'] . " AND deletestatus = '' ORDER by pageorder";
        $innerResult = mysql_query($query);

        while ($innerResult && $row = mysql_fetch_assoc($innerResult)) {
               echo '<option value="'.$row['pageid'].'"';
               if ($row['pageid'] == $current_parent_id) { echo ' selected="selected"'; }
               echo '>&nbsp;&nbsp;&nbsp;&nbsp;'.$row['pagetitle'];
               echo '</option>';
        }
  		}
  		echo "</select>\n";
 		?>

Edit Item.
-------------------------------------

<?php
    echo '
    <tr>
      <td class="textstandard">Assign to Page</td>
      <td><span class="textstandard"><img src="images/icon_circlearrow.gif" alt="" width="11" height="11" /></span></td>
      <td>';

    $current_page_id = intval($_GET['pageid']);
     
    $sql = "SELECT parentid FROM pages WHERE pageid = '".$current_page_id."'";
    $crs = mysql_query($sql);
    $current_parent_id = mysql_result($crs, 0, 0);
     
    $rs = mysql_query("SELECT  * FROM pages p1 WHERE NOT p1.pageid = '".$current_page_id."' AND (p1.parentid='' OR p1.parentid IS NULL) AND deletestatus = '' ORDER by pageorder ");
    if (mysql_num_rows($rs) > 0)
    {
        //Then loop within the results:
        echo "<select name='ud_parentid' class='gradientform'>\n";
        echo "<option value=''>Main Menu\n";
        while ($rows = mysql_fetch_assoc($rs))
        {
           echo '<option value="'.$rows['pageid'].'"';
           if ($rows['pageid'] == $current_parent_id) { echo ' selected="selected"'; }
           echo '>'.$rows['pagetitle'];
           echo '</option>';
           
           $query = "SELECT pageid, pagetitle FROM pages WHERE parentid=" . $rows['pageid'] . " AND deletestatus = '' ORDER by pageorder ";
           $innerResult = mysql_query($query);
           while ($innerResult && $row = mysql_fetch_assoc($innerResult)) {
               echo '<option value="'.$row['pageid'].'"';
               if ($row['pageid'] == $current_parent_id) { echo ' selected="selected"'; }
               echo '>&nbsp;&nbsp;&nbsp;&nbsp;'.$row['pagetitle'];
               echo '</option>';
           }
        }
        echo "</select>";
    }
    
   

 
      
?>

Open in new window

0
Cheryl Lander
Asked:
Cheryl Lander
  • 2
1 Solution
 
dirknibleckCommented:
Your first query has an OR that is a semi-problem. Basically you need brackets...

SELECT pageid, pagetitle, parentid FROM pages WHERE (parentid='' or Parentid is null) AND deletestatus = '' ORDER by pageorder

Open in new window


Without the brackets, the query will include any pages where parentid ='' regardless of deletestatus because if parentid = '' is true, the OR condition is satisfied. By default the WHERE clause will be treated like (parentid ='') OR (Parentid is null AND deletestatus='')
0
 
Cheryl LanderAuthor Commented:
perfect.

Thanks a lot.
0
 
Cheryl LanderAuthor Commented:
Perfect.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now