Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    if - else with isset for variable confirmation and/or replacement

    I am creating a search application with a 1) search form, 2) search results list (with links), and 3) a full document display. Once the search criteria is established and a specific document has been selected from the list, the full document is loaded based on the "FullDocumentID" field that is passed by the GET method into the URL. This all works fine, except at the beginning when no "FullDocumentID" is present in the URL. So I would like to include an if - else with isset to confirm that the FullDocumentID is in the URL. If not, I want a default document loaded (FullDocumentID = 1).

    My approach is to qualify the db query like this:

    if(isset($_GET['FullDocumentID']))
    {
    $doc = mysql_query(
    "SELECT
    SectionText
    FROM Full_Documents
    WHERE FullDocumentID = " . $_GET['FullDocumentID']);;
    else
    $_GET['FullDocumentID'] = '1';
    }

    But I get this error:

    Parse error: syntax error, unexpected T_ELSE in /home/content/36/10807936/html/ecr/test4.php on line 44

    Line 44 is blank following the last line of the above code. I have tried many variations, but need some help. Is there a better way to achieve my objective? Or is this approach feasible, but just need to tweak the code? Any help with this would be greatly appreciated.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    try this
    PHP Code:
    <?php
    if(isset($_GET['FullDocumentID'])){
        
    $doc mysql_query("SELECT
        SectionText
        FROM Full_Documents
        WHERE FullDocumentID = " 
    $_GET["FullDocumentID"]);
    }else{
        
    $_GET['FullDocumentID'] = '1';
    }
    ?>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your prompt and helpful reply. Your suggested code did fix the coding error message from my faulty attempt at if - else. A good learning experience for me.

    But I am back to where I began with regard to the original issue that I am getting an error message indicating that no variable is found for the document display (FullDocumentID) that determines which document is shown. After the search form is used and an item from the results list is selected, the error message goes away and the proper full document is displayed, as before. Here is the error message that I get when first loading the web page:

    Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/content/36/10807936/html/ecr/test4.php on line 104

    So I am wondering if I was mistaken in trying the if -else approach that you helped me with. Here is the full code for the application in case you wish to make further suggestions on what I need to do to fix this. Thanks again for the help you have provided already.

    <?php

    ini_set('display_errors',1);
    error_reporting(E_ALL);

    $doctype = isset($_GET['doctype']) ? $_GET['doctype'] : '';
    $search = isset($_GET['search']) ? $_GET['search'] : '';

    if($search){
    mysql_connect("xx", "xx", "xx") or die(mysql_error());
    mysql_select_db("xx") or die(mysql_error());

    $result = mysql_query(
    "SELECT
    FullDocumentID,
    CONCAT(ReadingNo, '-', SequenceNo) AS full_doc_number
    FROM Full_Documents
    WHERE SectionText LIKE '%$search%' AND SectionTypeID = '$doctype'
    Order By FullDocumentID");

    $num_records = mysql_num_rows($result);

    // this code expects the result resource from the query to be in $result
    if(mysql_num_rows($result) > 0){
    $links = array();
    while ($row = mysql_fetch_assoc($result)){
    $links[] = $row;
    }
    }
    }

    if(isset($_GET['FullDocumentID'])){
    $doc = mysql_query("SELECT
    SectionText
    FROM Full_Documents
    WHERE FullDocumentID = " . $_GET["FullDocumentID"]);
    }else{
    $_GET['FullDocumentID'] = 1;
    }

    ?>

    <html>
    <head>
    <style type="text/css">
    .highlight_word{
    background-color: yellow;
    }
    </style>
    </head>
    <body style="padding: 10px; background-color: white;">

    <div id="container" style="padding: 10px">

    <div id="search-results" style="position: absolute; left: 10px; top: 10px; width: 400px; height: 700px; padding:20px; background-color: white;">

    <h3>Search Form</h3>

    <form method="get" action="" id="search_criteria">
    Search Term <input type="text" id="" name="search"></br>
    Document Type <select name="doctype"></br>
    <option value="1">Text</option>
    <option value="3">Background</option>
    <option value="2">Reports</option>
    <option value="4">Index</option>
    </select></br>
    <input type="submit" name="submit" value="Submit">
    </form>

    <h3>Search Results</h3>

    <p>for: <span style="font-size: 14px; font-weight: bold; color: red;"><? echo $search; ?></span> </p>
    <p>documents found: <span style="font-size: 14px; font-weight: bold; color: red;"><? echo $num_records; ?></span></p>

    <?php
    if($search){
    if(isset($links)){
    // output document links
    echo 'Select a document to view:<br>';
    foreach($links as $link){
    $_GET['FullDocumentID'] = $link['FullDocumentID']; // add document id to (any) existing get parameters
    $qs = http_build_query($_GET, '', '&amp;'); // build the current url query string
    echo "<a href='?$qs'>{$link['full_doc_number']}</a><br>";
    }
    } else {
    echo 'No matching documents found.';
    }
    }
    ?>

    </div> <!--end of search and results div-->

    <!--Beginning of Document Text Div-->

    <div id="documenttext" style="position: absolute; right: 10px; top: 10px; width: 800px; height: 700px; background-color: white;">

    <h3>Document Text</h3>

    <?php
    //DOCUMENT DATA DISPLAY BEGINS HERE//
    echo "<table border='1' width='800px'>
    <tr class=\"style3\">
    ";
    while($row = mysql_fetch_array($doc))
    {
    echo "<tr class=\"style2\">";
    echo "<td style=\"width: 700px; padding: 10px;\"><p>" . preg_replace("/\r\n|\r|\n/", "</p><p>", $row['SectionText']) . "</p></td>";
    echo "</tr>";
    }
    echo "</table>";
    ?>

    </div> <!--END OF DOCUMENT TEXT DIV-->
    </div> <!--END OF CONTAINER DIV-->
    </body>
    </html>

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    The error tells me you are not getting a return from your query. Check the value of $_GET["FullDocumentID"]. See if you have one and if not set one so the query can work.
    If you set $_GET["FullDocumentID"] to a authentic number it should work.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Your document display logic is trying to use the result from a query that hasn't even been run because a document link has not been clicked on yet.

    In the other help site where someone gave you the logic to combine the functionality on one page, they had some conditional statements test and insure that a result only got used when it existed. Where is that logic in your current code?

    One of the points of the separation of the business and presentation logic that was presented (pun intended) is that the business logic is the only place that knows where the data is stored or how to retrieve it. You should not have any database mysql_XXXXXX() statements in the presentation logic.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    @CFMaBiSmAd,
    The query is run if $_GET['FullDocumentID'] is set. It doesn't need a document link to be clicked on.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    $_GET['FullDocumentID'] COMES from a document link being clicked on.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Just above the table that displays the full document (where the error message shows), I put this code:

    echo ($_GET['FullDocumentID']);

    When I refresh the page so that there is no FullDocumentID Get data in the URL, the number 1 is returned from my echo statement. So the if - else seems to be working and making the hardcoded FullDocumentID (1) available in the absence of anything in the browser url. But still the error message and no document displayed with the FullDocumentID of 1.

    I thought maybe I needed to reverse the if -else, so I tried empty instead of isset and reversed the order like this:

    if(empty($_GET['FullDocumentID'])){
    $_GET['FullDocumentID'] = 1;
    }else{
    $doc = mysqli_query($con,
    "SELECT
    SectionText
    FROM Full_Documents
    WHERE FullDocumentID = " . $_GET["FullDocumentID"]);
    }

    But no change - I still get the error message until I actually select a cell from the search results list and the FullDocumentId shows up in the browser url.

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    It seems I gave you too much credit and CFMaBiSmAd was on the right track.
    You can't refresh this page and get anywhere. $_GET['FullDocumentID'] yells that something is sent to this page from ANOTHER PAGE and you can only get this page to work properly if you run the other page and plug info into whatever.
    OR you put something like $_GET["FullDocumentID"] = 34; at the top of your page like I asked in post#4. I'm just guessing that you have 34 someplace in your DB under column FullDocumentID. If not use a number you do have.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #10
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,027
    Thanks
    2
    Thanked 315 Times in 307 Posts
    The page itself produces the links that set $_GET['FullDocumentID'].

    Since the purpose of that block of code is to take the $_GET['FullDocumentID'] value and get the corresponding SectionText out of the database table, why do you even have an else statement there? Makes no sense. And if the else statement has anything to do with trying to prevent an error somewhere else in the code, how could setting $_GET['FullDocumentID'] to a one possibly prevent an error when the only code that expects $_GET['FullDocumentID'] to have something in it, is that particular block of code?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #11
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes, don't need the else statement. Just this:

    if(empty($_GET['FullDocumentID'])){
    $_GET['FullDocumentID'] = 1;
    }

    Solves it nicely. Thanks for your help!

  • #12
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Just my 2 pence for what it's worth but I would myself be inclined to use a separate variable to contain the FullDocumentID. I.e
    $FullDocID = $_GET['FullDocumentID'];
    if(empty($FullDocID])){
    $FullDocID =1;
    }

    And then use the new variable in your SQL statement. It is not really the best practice to set $_GET variables as these are only really meant for server URL values.

  • Users who have thanked Fehnris for this post:

    dave1950 (04-16-2014)

  • #13
    New to the CF scene
    Join Date
    Apr 2014
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That's a great suggestion and I just incorporated into the code, which I am converting to PDO and working to make conform more to the Business/Presentation Logic model mentioned previously. I think I can probably learn a lot from this project if it gets completed with all the features I have in mind. Thanks again to you and the others who have contributed to my education.

  • #14
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    Your welcome dave1950. Great thing about forums is being able to help fellow programmers into improving their skills


  •  

    Posting Permissions

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