need help mysql_fetch_array on dropdown selection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

need help mysql_fetch_array on dropdown selection

Post by wolfwood16 »

hi there.

i am currently having a hard time solving this stuff. This is the scenario....

The student must select a curriculum year (ex. 2007-2008) on a dynamic dropdown list for him to view the subjects on that curriculum. This curriculum are tables on the database. The dropdown is automatically updating everytime the admin added a new curriculum. The value of the dropdown list must be encoded with strtolower(), strip_tags(), and str_replace() because the curricula added by the admin are also encoded with these functions.

code to generate the curriculum year dropdown list:

Code: Select all

 
<form id="formStatus" name="currentStatus" method="post" action="includes/validateStatus.php">
 <select name="selectedCurriculumIs" id="select">
                      [color=#BF0000]<?php[/color]
                      [color=#0000FF]include_once[/color] 'admin_/conn.php';
                      
                     [color=#400000] $retrieveCurrs[/color] = [color=#008000]"SELECT * FROM curr_names"[/color];
                      [color=#400000]$retrieveNow[/color] = mysql_query([color=#400000]$retrieveCurrs[/color]);
                      
                      [color=#0000FF]while[/color]($resultCurr = mysql_fetch_array([color=#400000]$retrieveNow[/color])){
                        echo "<option value='[color=#400000]$resultCurr[/color][id]'> [color=#400000]$resultCurr[/color][curryear] </option>";
                      }
                      
                        
                     [color=#BF0000]?>[/color]
                      </select>
                      <input type="hidden" name="isSubmittingStatus" value="yes">
                      <input type="submit" class="submitAssess" value="Submit Status"/>
                    </p>
                  </form>
 
the user must select one of the curriculum year, for him to view his needed subjects for his year level and pass it to includes/validateStatus.php

validateStatus.php

Code: Select all

 
if (![color=#0000FF]isset[/color]([color=#400000]$_SESSION[/color])) {
    session_start();
}
if ([color=#400000]$_REQUEST[/color][[color=#408000]'isSubmittingStatus'[/color]] == [color=#008000]"yes"[/color]) {
    if([color=#400000]$_GET[/color][[color=#008000]'id'[/color]]){
           [color=#0000FF]include_once[/color] '../admin_/conn.php';
    
       [color=#400000]$id[/color] = (int) $_GET['id'];
       [color=#400000]$getCurr[/color] = [color=#0000FF]mysql_query[/color]("SELECT currname FROM curr_names WHERE curryear = '$id'");
    
       [color=#400000]$_SESSION[/color][[color=#008000]'selectedCurr'[/color]] = strtolower(strip_tags(str_replace(' ', '', [color=#400000]$getCurr[/color])));
 
        }
}    
 
the user will go through couple of pages until he get to the view page of his needed curriculum subjects so i set it to a session handler named $_SESSION['selectedCurr']

This is the view page

Code: Select all

 
if (![color=#0000FF]isset[/color]([color=#400000]$_SESSION[/color])) {
    session_start();
}
 
include_once '../../admin_/conn.php';
                    
$retrieveFirstSem = "SELECT * FROM ".$_SESSION['selectedCurr']." WHERE subj_sem = '1' AND subj_level = '1'";
$returnFS = mysql_query($retrieveFirstSem);
                    
while($fs = mysql_fetch_array($returnFS)){
    echo "$fs[subj_code]";
    echo "$fs[subj_desc]";
    echo "$fs[lec_hrs]";
    echo "$fs[lab_hrs]";
    echo "$fs[subj_units]";
    echo "$fs[subj_preq]";
                        
}
 
And it give me an error of

Code: Select all

resourceid#4
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xamppserv\htdocs\subjass\fresh\comsci\index.php on [color=#FF0000]line 59[/color]
The resourceid#4 was the result after i've echoed the $_SESSION['selectedCurr']. The line 59 on the view page is

Code: Select all

 
$retrieveFirstSem = "SELECT * FROM ".$_SESSION['selectedCurr']." WHERE subj_sem = '1' AND subj_level = '1'";
 
I bet it's on the session handler of the curriculum name that was generated on the validateStatus.php

Anyone please help, i really need a hand...Thank you so much in advance and God bless us
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: need help mysql_fetch_array on dropdown selection

Post by yacahuma »

wolfwood16

Why do people keep putting sql code with html code? Is it because thats the only ways they do it in tutorials?


Rephrasing

The users selects a year and the curriculum for that year are displayed?

What is the id of the curriculum table?
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: need help mysql_fetch_array on dropdown selection

Post by wolfwood16 »

Why do people keep putting sql code with html code? Is it because thats the only ways they do it in tutorials?
yes sir as a beginner, i prefer to use it as it was stated on tutorials.
The users selects a year and the curriculum for that year are displayed?
this is the scenario, when the admin adds a new curriculum (examplecurriculum), the program will create a new table (example_curriculum) and the name will save to another table (curr_names). the examplecurriculum contains fields:

Code: Select all

id INT  
subj_code VARCHAR, 
subj_desc VARCHAR, 
lec_hrs INT, 
lab_hrs INT, 
subj_units INT, 
subj_preq VARCHAR, 
subj_sem INT, 
subj_level INT
In this way, it will be easy for me to retrieve the fields of the newly created curricula because their name and the values from the curr_names table are just the same. As i've said these names are encoded with

Code: Select all

$_SESSION['selectedCurr'] = strtolower(strip_tags(str_replace(' ', '', $getCurr)));
so for example, the admin will create "Newly Revised Curriculum 2008", it will become "newlyrevisedcurriculum2008". This will also be the name of the table i want to retrieve. The "Newly Revised Curriculum 2008" will be stored to curr_names(id, currname, currcourse, curryear).
What is the id of the curriculum table?
the id will be on the curr_names to be retrieve first by the student to be selected on the dropdown list.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: need help mysql_fetch_array on dropdown selection

Post by yacahuma »

Hold on,

Let me suggest something first. There is a reason is called a relational database. It is not a good idea to create a new table just because you are adding a new curriculum. You should look at how to design you database first. You could have hundreds of curriculums and you dont have to create a new table for each one. you can do it this way

CURR TABLE
ID_CURR, CURR_NAME,CURR_YEAR
1, CURR_NAME, 2007
2, CURR_NAME, 2008

CURR_SUBJECT
ID_CURR, subject
1,math, etc
1,english, etc
2,math, etc

does this make sense?
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: need help mysql_fetch_array on dropdown selection

Post by wolfwood16 »

thanks for the suggestion sir, i'll try to write again the codes similar to your suggestion
, but is there a fix to make my code to work?
wolfwood16
Forum Commoner
Posts: 43
Joined: Wed Aug 27, 2008 8:52 am
Contact:

Re: need help mysql_fetch_array on dropdown selection

Post by wolfwood16 »

ok' ive just revised the program...I've created a new table named curr_subjs with the ff fields:

Code: Select all

 
id INT  
subj_code VARCHAR,
subj_desc VARCHAR,
lec_hrs INT,
lab_hrs INT,
subj_units INT,
subj_preq VARCHAR,
subj_sem INT,
subj_level INT
subj_curr VARCHAR
 
now, if the admin created a new curriculum, the name(for example, The New Curriculum) of that curriculum will be stored in curr_names.When he add new subjects, those subjects will be stored in curr_subjs but the value for subj_curr field will be the modified one(thenewcurriculum).

the code is working properly...

but i have a situation which i cant handle regarding on retrieving the data from the table curr_subjs. The code returns no error, but ain't working

on the admin page, there's an option to view the added curriculum, here's the code

Code: Select all

 
include_once 'conn.php';
$getCurriculum = "SELECT * FROM curr_names";
$getCurriculumNow = mysql_query($getCurriculum);
while($crclm = mysql_fetch_array($getCurriculumNow)){
         print<<<HERE
                 <tr>
              <td width="493" height="27" valign="middle" style="background:#ffffff; color:#333333; padding-left: 10px;">$crclm[currname]</td>
          <td width="80" valign="middle" style="background:#ffffff; color:#333333; padding-left: 10px;">$crclm[currcourse]</td>
          <td width="71" valign="middle"><form name="form2" method="post" action="[color=#80FF00]viewcurr.php?mode=View&viewCurrIs=[/color][color=#800000]$crclm[/color][color=#40FF00][id][/color]"><input type="submit" class="qpost_button" name="LetsViewCurr" value="View" /></form></td>
         </tr>
HERE;
 
}
 
Upon selecting his decided curriculum he want to view, the page will bring him to viewcurr.php

Code: Select all

 
//on the top of the page
if(!isset($_SESSION)){
    session_start();
}
 
 
if ($_REQUEST['mode'] == "View") {
    include_once '../admin_/conn.php';
    if ($_GET['id']) {
        $id = (int)$_GET['id'];
        
        $getCurriculumNow = mysql_query("SELECT currname FROM curr_names WHERE id = '$id'");
        
        $strippedCurrName = strtolower(strip_tags(str_replace(' ', '', $getCurriculumNow)));
        echo ($getCurriculumNow." and ".$strippedCurrName) ;
    }
}
 
get the id of the selected curriculum, and returns the unmodified name(The New Curriculum), and modify it with the $strippedCurrName variable so the name now will be (thenewcurriculum).


and on the result section

Code: Select all

 
[color=#800000]$GetSubjsF[/color] = "SELECT * FROM curr_subjs WHERE subj_curr = '$strippedCurrName' AND subj_level = 1 AND subj_desc != '' AND subj_sem = 1";
 
[color=#400000]$GetSubjsNowF[/color] = mysql_query([color=#800000]$GetSubjsF[/color]);
 
while ($subjSecondSemView = mysql_fetch_array($GetSubjsNowS)) {
echo "$subjSecondSemView[subj_code] ";
echo "$subjSecondSemView[subj_desc] ";
echo "$subjSecondSemView[lec_hrs] ";
echo "$subjSecondSemView[lab_hrs] ";
echo "$subjSecondSemView[subj_units] ";
echo "$subjSecondSemView[subj_preq]";
}
 

again, the code did not return any error, but not functioning...

Please help me solve this....Many thanks in advance and God bless us always...
Post Reply