Jump to content

Sort Drop Down List populated with MYSQL table + Add Links from same


abhic

Recommended Posts

Hello Guys

I am using Pligg Beta 9 and got this code to create a drop-down list from the list of stories in that table.

What I want to do now is to

 

Add a header to the drop down like - "Select Story"

Sort stories alphabetically

Get html links that are stored in those tables for the appropriate story and when the user selects that story, the page goes to that link

 

I have also included the format of the table.

 

Any help will be appreciated.

 

Thank you.

 

Table Structure:

CREATE TABLE `pliggb9_links` (
  `link_id` int(20) NOT NULL auto_increment,
  `link_author` int(20) NOT NULL default '0',
  `link_blog` int(20) default '0',
  `link_status` enum('discard','queued','published','abuse','duplicated') NOT NULL default 'discard',
  `link_randkey` int(20) NOT NULL default '0',
  `link_votes` int(20) NOT NULL default '0',
  `link_karma` decimal(10,2) NOT NULL default '0.00',
  `link_modified` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `link_date` timestamp NOT NULL default '0000-00-00 00:00:00',
  `link_published_date` timestamp NOT NULL default '0000-00-00 00:00:00',
  `link_category` int(11) NOT NULL default '0',
  `link_lang` int(11) NOT NULL default '1',
  `link_url` varchar(200) NOT NULL default '',
  `link_url_title` text,
  `link_title` text NOT NULL,
  `link_title_url` varchar(255) default NULL,
  `link_content` text NOT NULL,
  `link_summary` text,
  `link_tags` text,
  `link_field1` varchar(255) NOT NULL default '',
  `link_field2` varchar(255) NOT NULL default '',
  `link_field3` varchar(255) NOT NULL default '',
  `link_field4` varchar(255) NOT NULL default '',
  `link_field5` varchar(255) NOT NULL default '',
  `link_field6` varchar(255) NOT NULL default '',
  `link_field7` varchar(255) NOT NULL default '',
  `link_field8` varchar(255) NOT NULL default '',
  `link_field9` varchar(255) NOT NULL default '',
  `link_field10` text NOT NULL,
  `link_field11` text NOT NULL,
  `link_field12` text NOT NULL,
  `link_field13` text NOT NULL,
  `link_field14` text NOT NULL,
  `link_field15` text NOT NULL,
  PRIMARY KEY  (`link_id`),
  KEY `link_author` (`link_author`),
  KEY `link_url` (`link_url`),
  KEY `link_date` (`link_date`),
  KEY `link_published_date` (`link_published_date`),
  FULLTEXT KEY `link_url_2` (`link_url`,`link_url_title`,`link_title`,`link_content`,`link_tags`),
  FULLTEXT KEY `link_tags` (`link_tags`)
) ENGINE=MyISAM AUTO_INCREMENT=61 DEFAULT CHARSET=latin1;

 

System Table Load Code (to alphabetize and add links here?)

//this gets all the stories
    $storySql = "SELECT link_id, link_title from " . table_links . " where link_status = 'published' or link_status = 'queued';";
    $stories = $db->get_results($storySql);
    //store the id and title in an array
    $storylist = array();
    foreach ( $stories as $story )
        $storylist[] = array($story->link_id,$story->link_title);
    //pass the array to the template
    $var_smarty->assign('dropdownStories',$storylist);  

 

Drop Down List Code (need ajax?)

<li>
    <div class="box" id="dropdownstory">
        <select>
            {foreach from=$dropdownStories value=dropdownstory}
            <option value="{$dropdownstory[0]}">{$dropdownstory[1]}</option>
            {/foreach}
        </select>
    </div>
</li>  

Link to comment
Share on other sites

To add the header you'll probably need to make the following addition.

$storylist = array();
$storylist[] = array(0, "Select Story");

 

To sort the rows you can use the following syntax

SELECT .. FROM tablename WHERE ... ORDER BY columnname ASC

 

ASC (ascending) is the default and you can also sort DESC (descending).

 

Get html links that are stored in those tables for the appropriate story and when the user selects that story, the page goes to that link

 

After the user submits the form you can use the link_id to retrieve the associated url and then redirect the user to the new page.

 

eg.

$id = (int) $_GET['dropdownstory'];
$query = "SELECT link_url FROM table WHERE link_id = $id";
.....
header('Location: '. $story->link_url);
.....

 

The user can also be sent to the next page automatically after selecting the entry using javascript.

Link to comment
Share on other sites

//this gets all the stories
    $storySql = "SELECT link_id, link_title from " . table_links . " where link_status = 'published' or link_status = 'queued' ORDER BY link_title ASC;";
    $stories = $db->get_results($storySql);
    //store the id and title in an array
    $storylist = array();
$storylist[] = array(0, "Select your story");
    foreach ( $stories as $story )
        $storylist[] = array($story->link_id,$story->link_title);
    //pass the array to the template
    $var_smarty->assign('dropdownStories',$storylist); 

 

Thank you.

 

I have this code. This sorts the stories.

 

Now what I want to do is add the links. Can you show me exactly how to do that? I am a bit confused with what you posted.

 

Thank you so much.

Link to comment
Share on other sites

I am getting this error message:

 

Warning: Cannot modify header information - headers already sent by (output started at /public_html/pligg/index.php:2) in /public_html/pligg/libs/html1.php on line 147

 

Nothing should be output to the browser before a header, which is what header(Location...) does. The error shown says that there is output on line 147 of html1.php. You'll need to remove that. If the logic of your script doesn't permit that, you can put ob_start() at the start of the script as an alternative.

Link to comment
Share on other sites

What is the logic/code for adding the links? Could you post that exact code please on the display side and the mysql side?

 

Thank you.

 

The select box should allow the user to make a selection in a form. The form should be submitted with a "Submit" button. If the page being submitted to is the same page it should have logic similar to the following

 

Note that $_GET may be $_POST depending on the "action" attribute of the form.

if (isset($_GET['dropdownstory']))
{
    $id = (int) $_GET['dropdownstory'];
    $query = "SELECT link_url FROM table WHERE link_id = $id";
    //retrieve the "link_url" from the result and use it with the header() function.
    header ('Location: '.$link_url);
}
else
{
    //display drop down links

}

 

Note also that there should be code to handle an instance where no match is found.

 

If you're still having problems post the code in its current form.

Link to comment
Share on other sites

There are two different files:

One in base system and other in the template folder:

 

Base System page has this code:

//this gets all the stories
    $storySql = "SELECT link_id, link_title from " . table_links . " where link_status = 'published' or link_status = 'queued' ORDER BY link_title ASC;";
    $stories = $db->get_results($storySql);
    //store the id and title in an array
    $storylist = array();
$storylist[] = array(0, "Select your story");
    foreach ( $stories as $story )
        $storylist[] = array($story->link_id,$story->link_title);
//pass the array to the template
    $var_smarty->assign('dropdownStories',$storylist);  
if (isset($_GET['dropdownstory']))
{
    $id = (int) $_GET['dropdownstory'];
    $query = "SELECT link_url FROM table WHERE link_id = $id";
    //retrieve the "link_url" from the result and use it with the header() function.
    header ('Location: '.$link_url);
}
else
{
    //display drop down links

}

 

Template Page has this code:

 

<h2>Story Listings</h2>
    <div class="box" id="dropdownstory">
        <select>
            {foreach from=$dropdownStories value=dropdownstory}
            <option value="{$dropdownstory[0]}">{$dropdownstory[1]}</option>
            {/foreach}
        </select>
    </div> 
</h2>

 

 

I am still getting no linking. How would I get that to activate? I prefer some js over a 'submit' button. I am currently using prototype in the system, so if we could re-use to that, that would be great.

 

Link to comment
Share on other sites

If you'd like help with the javascript portion of your script you can post in the javascript forum. For simple navigation such as this however, I'd imagine it being more favourable to provide functionality with or without javascript being enabled.

 

If you're able to use your framework to create a submit button you should be able to use the following as a starting point to finishing the script.

//If you get header errors put "ob_start()" at the very beginning of your script.
if (isset($_GET['dropdownstory']))
{
    $id = (int) $_GET['dropdownstory'];
    $query = "SELECT link_url FROM table WHERE link_id = $id";
    $result = mysql_query($query) or die(mysql_error());
    if (mysql_num_rows($result))
    {
        list($link_url) = mysql_fetch_array($result);
        header ('Location: '.$link_url);
        exit();
    }
    else
    {
        echo 'invalid selection. Try again';
    }   
}

//this gets all the stories
    $storySql = "SELECT link_id, link_title from " . table_links . " where link_status = 'published' or link_status = 'queued' ORDER BY link_title ASC;";
    $stories = $db->get_results($storySql);
    //store the id and title in an array
    $storylist = array();
$storylist[] = array(0, "Select your story");
    foreach ( $stories as $story )
        $storylist[] = array($story->link_id,$story->link_title);
//pass the array to the template
    $var_smarty->assign('dropdownStories',$storylist);  

Link to comment
Share on other sites

Note also that this question was/is more appropriate for the PHP-Help forum and if you have other questions similar to this at a later date they should be directed to that forum. This forum deals primarily with the MYSQL side of things and does not delve deeply into the other areas.

Link to comment
Share on other sites

hello,

I put that in and didn't get any header errors.

 

so now basically I am to put a submit form in the frontview template page and call the link_url?

that should work?

 

thank you.

 

You need to create a form that will submit the values to the current page. I had originally assumed from your question that you had written this code yourself but If Pligg Beta 9 generates the code for you or you haven't written this code yourself I'm going to have to suggest that you read some of the tutorials on this and other sites.

 

If you're not familiar with what a form is or how it is used you can read through some of the tutorials on the web.

 

eg:

http://www.w3schools.com/html/html_forms.asp

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.