Jump to content

[SOLVED] php and html sort Question


astani

Recommended Posts

  Hi,

 

  I have a php planning (project) based site connected to a postgreSQL db working with Dreamweaver 8. I want to be able to sort the projects by project name or owner, by clicking on a dropdown and submitting.

  I still need an if statement somewhere to determine which queries will be called based on the dropdown selection.

Help.

 

Main page (projects page) - this is where I create the php dropdown and the html submit form to call the sort.php page with the queries:

 

//create php dropdown with two sort options

$sortBy = array(

1=> "Project Name",

2=> "Owner",

);

$category = str_replace(" ", " ", $sortBy);

 

echo '<SELECT name=category>';

foreach ($sortBy as $key => $value)

{

echo '<OPTION value='.$value.'> '.$value.'';

}

echo '</select>';

?>

<?php

  //print "<pre>"; var_dump($projByTitle); print "</pre>";

?>

  <!--create html form to execute queries on sort.php on clicking the submit button--> [/color]

  <form id="sort" action="sort.php" method="post" name= "sort">

  <input type="hidden" name="id" value="<? echo "$form_input[title]";?>" />

  <input type="submit" id="submit" name="submit" value ="submit"/>

<?

  ?>

 

</form>

<?  ?>

 

  On clicking submit button I call this code on sort.php queries:

 

<?php

include "header.php";

if($_SERVER['REQUEST_METHOD'] == "POST"){$form_input = $_POST;}elseif($_SERVER['REQUEST_METHOD'] == "GET"){$form_input = $_GET;}else{exit;}

 

 

 

$sort_query = pg_query("SELECT * FROM projects ORDER BY title");

$projByTitle = pg_fetch_array($sort_query);

$sort_query1 = pg_query("SELECT * FROM projects ORDER BY username");

$projByOwner = pg_fetch_array($sort_query1);

 

?>

 

<?

while (($row = pg_fetch_array($sort_query)) && $row1 = pg_fetch_array($sort_query1))

{

 

for ($i = 0; $i <= 1; $i++)

{

   

echo $row[$i] ?><br> <? ;

echo $row1[$i] ?><br> <? ;

//print "<pre>"; var_dump($projByTitle); print "</pre>";  

}

}

?>

 

I am attaching the sort.php file.

 

[attachment deleted by admin]

Link to comment
Share on other sites

I need some help please, how can I make my php dropdown output different results on submit? Is there a selectedindex value and how can I use it?

<?php

$sortBy = array(

1=> "Project Name",

2=> "Owner",

);

$category = str_replace(" ", " ", $sortBy);

 

echo '<SELECT name=category>';

foreach ($sortBy as $key => $value)

{

echo '<OPTION value='.$value.'> '.$value.'';

}

echo '</select>';

?>

Link to comment
Share on other sites

Your first problem seems to be here:

 

 

<?php
echo '<SELECT name=category>';

foreach ($sortBy as $key => $value)
 {
   echo '<OPTION value='.$value.'> '.$value.'';
} 

echo '</select>'; 
?>

 

You aren't building the dropdown correctly - missed a closing tag. Try this:

 

<?php
echo '<SELECT name=category>';

// I wasn't sure why you had $key => $value on the line below, so I removed it.
foreach ($sortBy as $value)
  {
    echo '<OPTION value='.$value.'> '.$value.'</option>';
  }

echo '</select>'; 
?>

 

If that doesn't fix it, I'll look at the rest of the code.

 

 

Link to comment
Share on other sites

My dropdown works fine I need a way to use an if statement and "selectedindex" in the dropdown to output different queries.

 

Below is code for Dropdown followed by html submit form:

 

//create php dropdown with two sort options

$sortBy = array(

1=> "Project Name",

2=> "Owner",

);

$category = str_replace(" ", " ", $sortBy);

 

echo '<SELECT name=category>';

foreach ($sortBy as $key => $value)

{

echo '<OPTION value='.$value.'> '.$value.'';

}

echo '</select>';

 

?>

<?php

  //print "<pre>"; var_dump($projByTitle); print "</pre>";

?>

  <!--create html form to execute queries on sort.php on clicking the submit button-->

  <form id="sort" action="sort1.php" method="post" name= "sort">

  <input type="hidden" name="id" value="<? echo "$form_input[title]";?>" />

  <input type="submit" id="submit" name="submit" value ="submit"/>

<?

Link to comment
Share on other sites

How do I use an if statement to output different queries once I submit the chosen option?

<select name="Sort">

            <option value= "Title">By Title</option>

            <option value="Owner" selected="selected">By Owner</option>

</select>

Link to comment
Share on other sites

To perform different queries based on what is selected, change the code here:

 

<?php
$sort_query = pg_query("SELECT * FROM projects ORDER BY title");
$projByTitle = pg_fetch_array($sort_query);
$sort_query1 = pg_query("SELECT * FROM projects ORDER BY username"); 
$projByOwner = pg_fetch_array($sort_query1);
?>

 

to something like

 

<?php
if($_POST['Sort'] == "Title") {
$sort_query = pg_query("SELECT * FROM projects ORDER BY title");
} elseif($_POST['Sort'] == "Owner") {
$sort_query = pg_query("SELECT * FROM projects ORDER BY username"); 
}

$result = pg_fetch_array($sort_query);
?>

 

That will run the appropriate query for you. You'll also need to change your loop in your original code.

Link to comment
Share on other sites

Still not working the way it should. The only way it works is if I do a query before the if statement which it seems to defeat the purpose. I am using php 4.3.9 on the server.

<html>

//html dropdown with submit form (button) connects to php page below it:

<form action ="" name = "sorting">

<select name ="sorting">

<option value="0" input name="byname" >By Name</option>

<option value="1" input name ="byowner">By Owner</option>

</select>

</form>

<form id="sort1" action="sort1.php" method="post" name= "sort">

<input type="hidden" name="id1" value = "sub" />

<input name ="submit1" input type="submit" id="submit" value ="submit"/>

        </form>

</html>

 

<?php

//conditional with queries based on dropdown choice by user

 

if($form_input['submit1'] == 0)

{

$sort_query = pg_query("SELECT * FROM projects ORDER BY title");

}

elseif($form_input['submit1'] == 1)

{

$sort_query = pg_query("SELECT * FROM projects ORDER BY username");

}

//looping thru to get the records displayed

 

while (($row = pg_fetch_array($sort_query)) && ($row != ""))

{

  for ($i = 0; $i <= 1; $i++)

   

echo $row[$i] ?><br> <? ;

//echo $row1[$i] 

//print "<pre>"; var_dump($projByTitle); print "</pre>";  

}

}

?>

Link to comment
Share on other sites

I don't understand your code... Surely this should do the same job with less code:

 

<html>

  <form id="sort1" action="sort1.php" method="post" name="sort">
      <select name ="sorting">
         <option value="0" input name="byname" >By Name</option>
         <option value="1" input name ="byowner">By Owner</option>
      </select><br />
      <input name="submit" input type="submit" id="submit" value ="submit"/>
  </form>
      
</html>

<?php

if($_POST['sorting'] == 0) 
   {
      $sort_query = pg_query("SELECT * FROM projects ORDER BY title");
   } 
elseif($_POST['sorting'] == 1) 
   { 
      $sort_query = pg_query("SELECT * FROM projects ORDER BY username");
   }

while ($row = pg_fetch_array($sort_query))
{
     for ($i = 0; $i <= 1; $i++)
       {  
         echo $row[$i]; 
	}
} 

?>

 

Your form confused me - why use two <form> elements? Also, where your while loop tailed off seemed to be doing something really weird...

Link to comment
Share on other sites

One more thing, how would I make the output get displayed by the letters of the alphabet as links..

 

I have the array to display the alphabet thus far but I don't know how to proceed:

 

<?php

$alph_array = array("A" => "A", "B"=>"B", "C" => "C", "D" =>"D", "E"=>"E","F"=>"F", "G"=>"G", "H"=>"H", "I"=>"I","J"=>"J", "K"=>"K","L"=>"L","M"=>"M","N"=>"N","O"=>"O","P"=>"P","Q"=>"Q","R"=>"R","S"=>"S","T"=>"T","U"=>"U","V"=>"V","W"=>"W","X"=>"X","Y"=>"Y","Z"=>"Z" );

$alph_array;

foreach ($alph_array as $value) {

  print_r("$value \n\n\n");}  ?>

Link to comment
Share on other sites

To alphabetise by project titles (if your exisiting code isn't doing it already):

 

<?php $sort_query = pg_query("SELECT * FROM projects ORDER BY title ASC"); ?>

 

You're getting there with the Alphabet as links...

 

<?php

// Your array here. I wouldn't bother with an associative one, but that is just a matter of preference.
$alph_array = array("A","B", /* You get the idea */,"Z" );

foreach($alph_array as $var) {
  echo "<a href='".$var."'>".$var."</a>  ";
}

?>

 

Of course, you'll also need to find a way of adding the anchors in the right place on the page as well (these things):

 

<a name='A'></a>

 

But have a think about that one yourself. I've got a rough idea bouncing around in my head something to do with detecting the value of the first character of the title that comes out of the DB and noting it, so that if the code detects a change, it can stick the next anchor in... Not entirely sure how i'd go about it yet though. You try!

 

Link to comment
Share on other sites

I tried this code and I get results and the links but I don't get the result on clicking individual links;

 

<?php

$alph_array = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","R","T","S","U","V","X","Y","Z" );

 

foreach($alph_array as $var) {

  echo "<a href='".$var."'>".$var."</a>  ";

}

for($u = 0; $u < count($alph_array); $u++)

{

if (($title[0]) == ($alph_array[$u])) {

    ?> <a name = "<? echo $alph_array[$u];?>" <?

$sorted_titles = pg_query("SELECT * FROM projects WHERE title LIKE '$alph_array[$u]'%'");?> onClick ="

<? echo (pg_fetch_array($sorted_titles)) ?>"></a><?

 

}

 

?>

Link to comment
Share on other sites

Oh, sorry, I thought you wanted all results on the page, and just anchor tags to jump down the page to the appropriate letter. If you want to display the results on clicking a letter, this may work:

 

<?php
  $alph_array = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","R","T","S","U","V","X","Y","Z" );

  foreach($alph_array as $var) { // For each value in the Array, run your query.
    $sorted_titles = pg_query("SELECT * FROM projects WHERE title LIKE '".$var."%'");
if(pg_num_rows($sorted_titles) >= 1) { // If the query returns results, display the letter as a link
      echo "<a href='#' onClick='".pg_fetch_array($sorted_titles)."'>".$var."</a>  ";
} else { // Otherwise just display the letter.
  echo $var."  ";
}
}
?>

 

Of course, I'm not sure if that onClick thing will work. You may need to point it at another page, where you'll need to run the query for that particular letter, so:

 

<?php 

echo "<a href='result.php?letter=".$var."'>".$var."</a>  "; 

// And on results.php

$letter = $_GET['letter'];
$sorted_titles = pg_query("SELECT * FROM projects WHERE title LIKE '".$letter."%'");

// Echo results in a while loop.

?>

 

See?

 

 

 

 

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.