Jump to content

Recommended Posts

This is some of the code from the page which displays results from a drop down selection

 

$result = conn($sql); 
  
  if (!$result){ die("No results due to database error.<br>".mysql_error());  } 
  
  if (mysql_num_rows($result)==0) 
  { 
    echo "No Results found!"; 
  }else{ 

echo "<TABLE  width=100% height=300 border='1' cellpadding='5' cellspacing='2'>";
while ($rows= mysql_fetch_array($result)) 
      
{ 

echo "<TR>";	
    echo "<TH colspan='5' scope='colgroup'>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</Th>";
  
    echo "</TR>";
	   
echo	  "<TR>";
    echo "<TH scope='col' abbr='beha'>Beha</TH>";
    echo "<TH scope='col' abbr='rating'>Rating</TH>";
    echo "<TH scope='col'>Kommentar</TH>";
  
  echo "</TR>";
     
     
   
      
echo "<TR>";
    echo "<TD width=20% height=100 scope='row'>". $rows['beha'] ."<p> ". $rows['navn'] ."<p> ". $rows['adresse'] ."<p> ". $rows['postnr'] .", ". $rows['by'] ."<p> ". $rows['tlf'] ."</TD>";
    echo "<TD width=30% height=100>". rating_bar($rows['id'],'6','static')."<a href='jadak.php?id={$rows['id']}&navn={$rows['navn']}&pr={$rows['pr']}&beha={$rows['beha']}'>Bedøm </a> </TD>";
    echo "<TD width=30% height=100><a href='komment.php?navn={$rows['navn']}&id={$rows['id']}&beha={$rows['beha']}'>Læs kommentarer</a> </TD>";
   
  echo "</TR>"; 
  
  
  
  



    }  
   

  
  echo "</table>";  } 

When there is only one result it is no problem, but when there are multiple results I get a problem. I only want this part

echo "<TR>";	
    echo "<TH colspan='5' scope='colgroup'>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</Th>";
  
    echo "</TR>";

to be at the top of the page and not at the top of every result it is showing. 

 

Link to comment
https://forums.phpfreaks.com/topic/201600-only-show-values-at-top-of-the-page/
Share on other sites

Just make a condition for that row to only display on the first run through the loop :

$result = conn($sql);
  if (!$result){ die("No results due to database error.<br>".mysql_error());  }
  if (mysql_num_rows($result)==0)
  {
    echo "No Results found!";
  }else{
echo "<TABLE  width=100% height=300 border='1' cellpadding='5' cellspacing='2'>";
$first_run = "true";
while ($rows= mysql_fetch_array($result))
{
   echo "<TR>";   
if ($first_run == "true"){
    echo "<TH colspan='5' scope='colgroup'>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</Th>";
$first_run = "false";
}
    echo "</TR>";
   echo     "<TR>";
    echo "<TH scope='col' abbr='beha'>Beha</TH>";
    echo "<TH scope='col' abbr='rating'>Rating</TH>";
    echo "<TH scope='col'>Kommentar</TH>";
  echo "</TR>";
   echo "<TR>";
    echo "<TD width=20% height=100 scope='row'>". $rows['beha'] ."<p> ". $rows['navn'] ."<p> ". $rows['adresse'] ."<p> ". $rows['postnr'] .", ". $rows['by'] ."<p> ". $rows['tlf'] ."</TD>";
    echo "<TD width=30% height=100>". rating_bar($rows['id'],'6','static')."<a href='jadak.php?id={$rows['id']}&navn={$rows['navn']}&pr={$rows['pr']}&beha={$rows['beha']}'>Bedøm </a> </TD>";
    echo "<TD width=30% height=100><a href='komment.php?navn={$rows['navn']}&id={$rows['id']}&beha={$rows['beha']}'>Læs kommentarer</a> </TD>";
  echo "</TR>";
    } 
     echo "</table>";  }

Thank you so much for your help. Maybe you can help me with another problem but on the same page.

After the user have chosen from the drop down

$first_run = "true";
while ($rows= mysql_fetch_array($result))
{
   echo "<TR>";   
if ($first_run == "true"){
    echo "<TH colspan='5' scope='colgroup'>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</Th>";
$first_run = "false";

} 

 

Shows on top of the page like you said. But what I want is to only show $rows['beha'] $rows['omraede'] $rows['problem'] if it is chosing from the dropdown. As you can see I’ve got three drop downs generated from mysql (beha, omraede, pr) The drop down also has a function which shows all, but if all is chosing it still displays (beha, omraede, pr) which is also the case if all is chosing from beha and a value is chosing from omraede and pr. So here it should only display omraede and pr. Hope you understand :=)

 

Maybe it should be something like this?

if ($beha == "true"){ echo ". $rows['beha'] ." else don’t print
if ($omraede == "true"){ echo ". $rows[' omraede '] ." else don’t print
if ($pr== "true"){ echo ". $rows[' pr '] ." else don’t print

 

Here is the full code


<?php 

/*------------------------------------------------------------------------ 
            control codes 
------------------------------------------------------------------------*/ 


if (isset($_POST['submit'])) 
{ 

  search();       //call the search function 

}else{ 

  show_form();   //call the show form function 

}//end if 

/*------------------------------------------------------------------------ 
            show the search form 
------------------------------------------------------------------------*/ 

function show_form() 
{ 
  //call the dropdown function which creates an html string to build a select box for each element 
  $beha   = dropdown('beha','be'); 
  $omraede  = dropdown('omraede','be'); 
  $pr     = dropdown('pr','be'); 


  
  
  
  echo "<form name='search' action=".$_SERVER['PHP_SELF']." method='post'> 
        <table width='50%' align='center' valign='center'> 
        
	<tr> 
          <td colspan='2' align='center'>Search Form</td> 
        </tr> 
        <tr> 
          <td align='right'>Beha:</td><td>$beha</td> 
        </tr> 
        <tr> 
          <td align='right'>Omraede:</td><td>$omraede</td> 
        </tr> 
        <tr> 
          <td align='right'>Pr:</td><td>$pr</td> 
        </tr> 
  
         
          <td colspan='2' align='center'> </td> 
        </tr> 
        <tr> 
          <td colspan='2' align='center'><input type='submit' name='submit' value='Go!'></td> 
        </tr> 

        </table> 
        </form>"; 

}//end function 


/*------------------------------------------------------------------------ 
            run the search and show the results 
------------------------------------------------------------------------*/ 

function search() 
{ 

//base sql 
  $sql = "select * from be WHERE 1" ; 

//get the values from the form 
//NOTE: You should do way more valdation on the values before you attempt to process anything 

  if ((!empty($_POST['beha']))&&($_POST['beha'] != 'all')) 
  { 
    $sql .= " and beha like '". addslashes($_POST['beha'])."%' "; 
  } 
  
  if ((!empty($_POST['omraede']))&&($_POST['omraede'] != 'all')) 
  { 
    $sql .= " and omraede like '". addslashes($_POST['omraede'])."%' "; 
  } 
  
  if ((!empty($_POST['pr']))&&($_POST['pr'] != 'all')) 
  { 
    $sql .= " and pr = '". addslashes($_POST['pr'])."' "; 
  } 
  

  
print "<table border=1>";


  //add more elements (or take away) as you desire...follow the same code structure as above 

    
  //run query 
$result = conn($sql);
  if (!$result){ die("No results due to database error.<br>".mysql_error());  }
  if (mysql_num_rows($result)==0)
  {
    echo "No Results found!";
  }else{
echo "<TABLE  width=100% height=300 border='1' cellpadding='5' cellspacing='2'>";
$first_run = "true";
while ($rows= mysql_fetch_array($result))
{
   echo "<TR>";   
if ($first_run == "true"){
    echo "<TH colspan='5' scope='colgroup'>Resultat for: <scope='row'>". $rows['beha'] .", ". $rows['omraede'] .", ". $rows['pr'] ."</Th>";
$first_run = "false";
}
    echo "</TR>";
   echo     "<TR>";
    echo "<TH scope='col' abbr='beha'>Behan</TH>";
    echo "<TH scope='col' abbr='rating'>Rating</TH>";
    echo "<TH scope='col'>Kommentar</TH>";
  echo "</TR>";
   echo "<TR>";
    echo "<TD width=20% height=100 scope='row'>". $rows['beha'] ."<p> ". $rows['navn'] ."<p> ". $rows['adresse'] ."<p> ". $rows['postnr'] .", ". $rows['by'] ."<p> ". $rows['tlf'] ."</TD>";
    echo "<TD width=30% height=100>". rating_bar($rows['id'],'6','static')."<a href='jadak.php?id={$rows['id']}&navn={$rows['navn']}&pr={$rows['pr']}&beha={$rows['beha']}'>Bedøm </a> </TD>";
    echo "<TD width=30% height=100><a href='komment.php?navn={$rows['navn']}&id={$rows['id']}&beha={$rows['beha']}'>Læs kommentarer</a> </TD>";
  echo "</TR>";
    } 
     echo "</table>";  }



  
  //end if 



}//end function 






/*------------------------------------------------------------------------ 
            create the drop downs 
------------------------------------------------------------------------*/ 

function dropdown($field, $table) 
{  
  //initialize variables 
  $oHTML  = ''; 
  $result = ''; 
  
  //check to see if the field is passed correctly 
  if (($field == "")||($table == "")) 
  { 
    die("No column or table specified to create drop down from!"); 
  } 

  $sql = "select distinct($field) from $table"; 
  
  //call the db function and run the query 
  $result = conn($sql); 

  //if no results are found to create a drop down return a textbox 
  if ((!$result) ||(mysql_num_rows($result)==0)) 
  { 
    $oHTML .= "<input type='text' name='$field' value='' size='15'>"; 
  }elseif (($result)&&(mysql_num_rows($result)>0)){ 
    
    //build the select box out of the results 
    $oHTML .= "<select name='$field'>\n<option value='all'>All</option>\n"; 
    while ($rows = mysql_fetch_array($result)) 
    { 
      $oHTML .= "<option value='".$rows[$field]."'>".$rows[$field]."</option>\n"; 
    } 
    $oHTML .= "</select>\n"; 
  } 
  
  //send the value back to the calling code 
  return $oHTML; 
}//end function 



?>

 

 

if ($beha == "true"){ echo ". $rows['beha'] ." else don’t print

if ($omraede == "true"){ echo ". $rows[' omraede '] ." else don’t print

if ($pr== "true"){ echo ". $rows[' pr '] ." else don’t print

= Syntax errors

 

if ($beha == "true") {
  echo $rows['beha'];
else if ($omraede == "true") { 
  echo $rows[' omraede '];
else if ($pr== "true") { 
  echo $rows[' pr '];
} else {}

This is what you wanted, I think? Although this will print the $row['w/e'], you would need to put it into the table.

 

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.