Jump to content

Help with Script


ndjustin20

Recommended Posts

Trying to figure out why a script I wrote, still new, isn't functioning the way I would like it too.  The problem comes in when I am trying to change the H2 information via reading the data from a mysql db.  I can get everything to work properly except I can't get the page=1 or 2 or 3 ect to store in the $_Get['page'] global variable or take more importantly take that id and do something with it.

 

Here is the script:

 


<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
    if(isset($_GET['subj'])){
        $sel_subj = get_subject_by_id($_GET['subj']);
        //$sel_page = NULL;
    }elseif(isset($GET['page'])){
        $sel_subj = NULL;
        $sel_page = get_page_by_id($_GET['page']); 
    }else{
        $sel_subj = NULL;
        $sel_page = NULL;
    }
    
    
    
?>
<?php include("includes/header.php"); ?>
            <table id="structure">
                <tr>
                        <td id="navigation">
                            <ul class="subjects">
<?php
        $subject_set = get_all_subjects();
            while($subject = mysql_fetch_array($subject_set)){
            echo "<li";
            if($subject["id"] == $sel_subj){
            
            echo  " class=\"selected\"";}
            echo "><a href=\"content.php?subj=" .  urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
            
        $page_set = get_pages_for_subject($subject["id"]);
            echo "<ul class=\"pages\">";
            while($page = mysql_fetch_array($page_set)){
            echo "<li><a href = \"content.php?page=" . urlencode($page['id']) . "\">{$page["menu_name"]}</a></li>";
            }
            echo "</ul>";
        }
    
?>
                        </ul>
                        </td>
                        <td id="page">
                                <h2>
                                
                                <?php
                                if(isset($sel_subj)){
                                echo $sel_subj['menu_name'];
                                $sel_page = NULL;
                                }elseif(isset($sel_page)){
                                    echo $sel_page['menu_name'];
                                    $sel_page = NULL;
                                }else{
                                    echo "Welcome to Wdiget Corp";
                                }
                                
                                ?>
                                </h2>
                              <?php  echo $sel_page; ?><br />
                              <?php  echo print_r($sel_page); ?>
                              
                        </td>
                    </tr>
                </table>
            </div>
    <?php require("includes/footer.php"); ?>


 

Here are the functions:


<?php
    //This file is the file to store all basic functions

    function confirm_query($result_set){
        
        if(!$result_set){
                die("Result failed " . mysql_error());
            }
        
    }


    function get_all_subjects(){
        Global $connection;
        $subject_query =    "SELECT *
                    FROM subjects
                    ORDER BY position ASC";
        $subject_set = mysql_query($subject_query, $connection);
        confirm_query($subject_set);
        return $subject_set;
    }
    
    function get_pages_for_subject($subject_id){
        Global $connection;
        $page_query =   "SELECT *
                            FROM pages
                            WHERE subject_id = {$subject_id}
                            ORDER BY position ASC";
            $page_set = mysql_query($page_query, $connection);
            confirm_query($page_set);
            return $page_set;
    }

    function get_subject_by_id($subject_id){
        global $connection;
        $query = "SELECT * ";
        $query .= "FROM subjects ";
        $query .= "WHERE id=" . $subject_id . " ";
        $query .= "LIMIT 1";
        $result_set = mysql_query($query, $connection);
        //Remember:
        //if no rows are returned, fetch_array will return false
        confirm_query($result_set);
        if ($subject = mysql_fetch_array($result_set)){
            return $subject;
        }else{
            return NULL;
        }
        
    }


    function get_page_by_id($page_id){
        global $connection;
        $query = "SELECT * ";
        $query .= "FROM pages ";
        $query .= "WHERE id=" . $page_id . " ";
        $query .= "LIMIT 1";
        $result_set = mysql_query($query, $connection);
        //Remember:
        //if no rows are returned, fetch_array will return false
        //confirm_query($result_set);
        //if ($page = mysql_fetch_array($result_set)){
            return $page;
       // }else{
            //return NULL;
            
        //}
    }

?>

 

 

Any help is much appreciated

Link to comment
https://forums.phpfreaks.com/topic/187067-help-with-script/
Share on other sites

:D

 

Ok i made that change first and then I got this error: Notice: Undefined variable: page in C:\wamp\www\widget_corp\includes\functions.php on line 64

 

I'm reposting the script as I made some changes.  I really appreciate the help.

 


<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
    if(isset($_GET['subj'])){
        $sel_subj = get_subject_by_id($_GET['subj']);
        $sel_page = NULL;
    }elseif(isset($_GET['page'])){
        $sel_subj = NULL;
        $sel_page = get_page_by_id($_GET['page']); 
    }else{
        $sel_subj = NULL;
        $sel_page = NULL;
    }
    
    
    
?>
<?php include("includes/header.php"); ?>
            <table id="structure">
                <tr>
                        <td id="navigation">
                            <ul class="subjects">
<?php
        $subject_set = get_all_subjects();
            while($subject = mysql_fetch_array($subject_set)){
            echo "<li";
            if($subject["id"] == $sel_subj){
            
            echo  " class=\"selected\"";}
            echo "><a href=\"content.php?subj=" .  urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
            
        $page_set = get_pages_for_subject($subject["id"]);
            echo "<ul class=\"pages\">";
            while($page = mysql_fetch_array($page_set)){
            echo "<li><a href = \"content.php?page=" . urlencode($page['id']) . "\">{$page["menu_name"]}</a></li>";
            }
            echo "</ul>";
        }
    
?>
                        </ul>
                        </td>
                        <td id="page">
                                <h2>
                                
                                <?php
                                if(isset($sel_subj)){
                                echo $sel_subj['menu_name'];
                                $sel_page = NULL;
                                }elseif(isset($sel_page)){
                                    echo $sel_page['menu_name'];
                                    $sel_page = NULL;
                                }else{
                                    echo "Welcome to Wdiget Corp";
                                }
                                
                                ?>
                                </h2>
                              
                              
                        </td>
                    </tr>
                </table>
            </div>
    <?php require("includes/footer.php"); ?>

Link to comment
https://forums.phpfreaks.com/topic/187067-help-with-script/#findComment-987846
Share on other sites

I assume it talking about this line:

echo "<li><a href = \"content.php?page=" . urlencode($page['id']) . "\">{$page["menu_name"]}</a></li>";

 

It is throwing a Notice to you, not exactly an error as the script will still parse completely with the notice. But this would mean that the variable $page is not defined when your script is using it, so there would be no value for $page['menu_name']. Check that you are using the correct variable name, no spelling or case errors etc. perhaps you've deleted a line that defined the variable and you didn't mean to?

 

 

On a second look, I would also be checking teh data your functions are returning and that they are what you are expecting.

Link to comment
https://forums.phpfreaks.com/topic/187067-help-with-script/#findComment-987857
Share on other sites

OMG!!!!  ok so i was looking in the wrong script.  I wasn't looking in the functions file on line 64 :D  I was looking in the content.php file.  Man I spent a long time trying to figure that out too :D  I'm brand new to php/mysql and still going through a course on lynda.com.  I really appreciate the help you guys gave me.  I have another issue also.  In the same script I can't make the subject name or the list item I select bold.  Inside the script you'll notice i use a selected class to make whatever is selected use that style from the css file.  I highlighted the information in the script below where it should be working though doesn't.

 


<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php
    if(isset($_GET['subj'])){
        $sel_subj = get_subject_by_id($_GET['subj']);
        $sel_page = NULL;
    }elseif(isset($_GET['page'])){
        $sel_subj = NULL;
        $sel_page = get_page_by_id($_GET['page']); 
    }else{
        $sel_subj = NULL;
        $sel_page = NULL;
    }
    
    
    
?>
<?php include("includes/header.php"); ?>
            <table id="structure">
                <tr>
                        <td id="navigation">
                            <ul class="subjects">
<?php
        $subject_set = get_all_subjects();
            while($subject = mysql_fetch_array($subject_set)){
            echo "<li";
           
if($subject["id"] == $sel_subj){
            
            echo  " class=\"selected\"";}
            echo "><a href=\"content.php?subj=" .  urlencode($subject["id"]) . "\">{$subject["menu_name"]}</a></li>";
            
        $page_set = get_pages_for_subject($subject["id"]);
            echo "<ul class=\"pages\">";
            while($page = mysql_fetch_array($page_set)){
            echo "<li><a href = \"content.php?page=" . urlencode($page['id']) . "\">{$page["menu_name"]}</a></li>";
            }
            echo "</ul>";
        }
    
?>
                        </ul>
                        </td>
                        <td id="page">
                                <h2>
                                
                                <?php
                                if(isset($sel_subj)){
                                echo $sel_subj['menu_name'];
                                $sel_page = NULL;
                                }elseif(isset($sel_page)){
                                    echo $sel_page['menu_name'];
                                    $sel_page = NULL;
                                }else{
                                    echo "Welcome to Wdiget Corp";
                                }
                                
                                ?>
                                </h2>
                              
                              
                        </td>
                    </tr>
                </table>
            </div>
    <?php require("includes/footer.php"); ?>

 

Ok for some reason when I am typing inside this text field it jumps all over the place and i can't see what i am typing or what i am selecting so I'm not able to highlight the above code.  Is there maybe a setting I need to change?

Link to comment
https://forums.phpfreaks.com/topic/187067-help-with-script/#findComment-987867
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.