SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 47

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link to website from query

    I have a table with four fields, one of them being Provider, when the provider results are shown via echo, i would like to be able to click the providers name which would then in turn take me to the providers website, please keep in mind not all providers will end in .com and some provider names are not all one word.

    any help appreciated!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    You need to have the link to the provider's website in your DB. Otherwise, how do you think to get the name of the website? Something like 'www.' + name provider + '.com'? And if their website has another name?

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    As guido2004 has already stated, you need to specify the providers URL in some way, as your already using a database this would seem the most appropriate.

    Here's a quick example.

    PHP Code:
    <?php
    $sSQL 
    'SELECT name, url FROM providers';
    $rResult mysql_query($sSQL);
    while (
    $aProvider mysql_fetch_assoc($rResult))
    {
        echo 
    sprintf('<a href="%s" title="%s" target="_blank">%s</a><br />',
            
    htmlentities($aProvider['url']),
            
    htmlentities($aProvider['name']),
            
    htmlentities($aProvider['name'])
        );
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Zealot FaridHadi's Avatar
    Join Date
    Nov 2008
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yupp, you'll have to add the URL's to the database. If you don't want to mess with the table that contains the information you already have, you can create a new table and link the two with the provider ID. This is a better option if some providers don't have a website.

  5. #5
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, i was also wondering if i have news posts and wanted an archive how id link to the full news article from the headings listed on a webpage? i would guess it would have to pick up the id from the heading to be clickable for the full news article?

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yes, you would have one script to display a list of the news items available, then another to display a specific news item.

    You would pass the id of each news item via the GET super global, something along these lines...

    PHP Code:
    <?php
    #displayNewsList.php
    $sSQL 'SELECT id, title FROM news';
    $rResult mysql_query($sSQL);
    while (
    $aNewsItem mysql_fetch_assoc($rResult))
    {
        echo 
    sprintf('<a href="displayNewsItem.php?newsid=%s" title="%s">%s</a><br />',
            
    $aNewsItem['id'],
            
    $aNewsItem['title'],
            
    $aNewsItem['title']
        );
    }

    #displayNewsItem.php
    if(isset($_GET['newsid']) && !empty($_GET['newsid']) && ctype_digit($_GET['newsid']))
    {
        
    $sSQL sprintf("SELECT title, content FROM news WHERE id = %s",
            
    $_GET['newsid']
        );
        
    $rResult mysql_query($sSQL);
        if(
    mysql_num_rows($rResult) === 1)
        {
            
    $aNewsItem mysql_fetch_assoc($rResult);
            echo 
    sprintf('<h3>%s</h3><p>%s</p>',
                
    htmlentities($aNewsItem['title']),
                
    htmlentities($aNewsItem['content'])
            );
            exit;
        }
    }
    echo 
    '<h3>Invalid News Item</h3>';
    echo 
    '<p>Sorry, the item you requested cannot be found.</p>';
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry i have only just got round to trying to add the archive but im struggling to get it working. On my right "sidebar" I have the code to display the article titles which is working the article titles are shown and the correct id's are there as when i hover over each article i can check.
    I have put the displaynewsitem code where i would want it shown (to the left of the sidebar where all main content is shown).

    Now straight off the bat before anything is clicked it shows the echoed

    "Invalid News Item
    Sorry, the item you requested cannot be found."

    even though i havent clicked one of the articles, secondly if i do click an article it goes to a dead link with the following in the url: www.mysite.com/displayNewsItem.php?newsid=6

    any ideas as to why this is happening?

    thanks silver

  8. #8
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simply add the URLs to the database

  9. #9
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry could you explain a little more as to what you mean?

    The news articles will constantly be added meaning, from your suggestion, every time news is posted i would go and add the url which somewhat defeats the purpose. Maybe you have not read any replies in the thread and your answering what was first asked and already replied to?

    thanks anyway

  10. #10
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry for the last reply. Can you please put the piece of code here?

  11. #11
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The code is already posted i have used exactly what Silverbullet posted, thanks.

  12. #12
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    please try this:

    #displayNewsItem.php
    if(isset($_GET['newsid']) && !empty($_GET['newsid']) && ctype_digit($_GET['newsid']))
    {
    die("if block executed");
    $sSQL = sprintf("SELECT title, content FROM news WHERE id = &#37;s",
    $_GET['newsid']
    );
    $rResult = mysql_query($sSQL);
    if(
    mysql_num_rows($rResult) === 1)
    {
    $aNewsItem = mysql_fetch_assoc($rResult);
    echo
    sprintf('<h3>%s</h3><p>%s</p>',
    htmlentities($aNewsItem['title']),
    htmlentities($aNewsItem['content'])
    );
    exit;
    }
    }else{
    die("else block executed");
    echo
    '<h3>Invalid News Item</h3>';
    echo
    '<p>Sorry, the item you requested cannot be found.</p>';
    }
    ?>

    let me know the output

  13. #13
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the invalid news item messages has disappeared and been replaced with "else block executed"

    Also the list of articles have disappeared from the right sidebar.....thanks

  14. #14
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(isset($_GET['newsid']) && !empty($_GET['newsid']) && ctype_digit($_GET['newsid']))
    the above condition is not getting true, therefore it is not entering the execution block of if.

    put these lines above the if block:
    if(isset($_GET['newsid'])) echo "<br> newsid exists";
    if( !empty($_GET['newsid'])) echo "<br>newsid contains value";
    if(ctype_digit($_GET['newsid'])) echo "newsid exists";

    let me know the output now.

  15. #15
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Parse error: syntax error, unexpected T_ELSE in /www/business/10675/s4ac-sample/news.php on line 85"

    line 84: }else{

  16. #16
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have you made any other change?

  17. #17
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    take this one.

    #displayNewsItem.php

    if(isset($_GET['newsid'])) echo "<br> newsid exists";
    if( !empty($_GET['newsid'])) echo "<br>newsid contains value";
    if(ctype_digit($_GET['newsid'])) echo "newsid exists";

    if(isset($_GET['newsid']) && !empty($_GET['newsid']) && ctype_digit($_GET['newsid']))
    {
    die("if block executed");
    $sSQL = sprintf("SELECT title, content FROM news WHERE id = &#37;s",
    $_GET['newsid']
    );
    $rResult = mysql_query($sSQL);
    if(mysql_num_rows($rResult) === 1)
    {
    $aNewsItem = mysql_fetch_assoc($rResult);
    echo sprintf('<h3>%s</h3><p>%s</p>',
    htmlentities($aNewsItem['title']),
    htmlentities($aNewsItem['content'])
    );
    exit;
    }
    }else{
    die("else block executed");
    echo '<h3>Invalid News Item</h3>';
    echo '<p>Sorry, the item you requested cannot be found.</p>';
    }
    ?>

  18. #18
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "else block executed"

  19. #19
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you should run the script like this:

    http://yourhost/displayNewsList.php?newsid=3

    you should know the exact newsid, if its not available then you will get error.

  20. #20
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i can no longer click any available titles as they disappeared with your code?
    but if i do enter it manually its a dead link.

  21. #21
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for testing purpose i used the die statement in my code to find out the error. So, the titles will not appear untill you remove die.

  22. #22
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you provide your current table structure with some sample data?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  23. #23
    SitePoint Zealot
    Join Date
    Nov 2008
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Table (News) consists of the following fields:
    id
    title
    date
    content
    author

    sample data? not sure what your after there, i literally have a few entires such as
    Title - news title
    content - lorem ipsum
    author - t3stin

    they show if i just echo them but i require the archive just showing titles so olders news items are clickable as only the newest 5 items are currently shown

  24. #24
    Non-Member salahsoft's Avatar
    Join Date
    Feb 2009
    Location
    India
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it will be better if you please export the mysql dump using phpmyadmin and paste the contents of mysql dump here.

  25. #25
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Give this a whirl, let us know what you get.

    PHP Code:
    <?php
    #displayNewsList.php
    $sSQL 'SELECT id, title FROM news';
    $rResult mysql_query($sSQL);
    while (
    $aNewsItem mysql_fetch_assoc($rResult))
    {
        
    printf('<a href="displayNewsItem.php?newsid=%s" title="%s">%s</a><br />',
            
    $aNewsItem['id'],
            
    $aNewsItem['title'],
            
    $aNewsItem['title']
        );
    }

    #displayNewsItem.php
    if(isset($_GET['newsid']) && !empty($_GET['newsid']) && ctype_digit($_GET['newsid']))
    {
        
    $sSQL sprintf("SELECT title, content FROM news WHERE id = %s",
            
    $_GET['newsid']
        );
        
    $rResult mysql_query($sSQL);
        if(!
    is_resource($rResult))
        {
            echo 
    '<h3>System Error</h3>';
            echo 
    '<p>' mysql_error() . '</p>';
            exit;
        }
        if(
    mysql_num_rows($rResult) === 1)
        {
            
    $aNewsItem mysql_fetch_assoc($rResult);
            
    printf('<h3>%s</h3><p>%s</p>',
                
    htmlentities($aNewsItem['title']),
                
    htmlentities($aNewsItem['content'])
            );
        }
        else
        {
            echo 
    '<h3>Invalid News Item</h3>';
            echo 
    '<p>Sorry, no article could be found matching your request.</p>';
        }
    }
    else
    {
        echo 
    '<h3>Invalid News Item</h3>';
        echo 
    '<p>Sorry, you failed to specify a valid article.</p>';
    }
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •