Jump to content

Issue with Loops


jayousama

Recommended Posts

I'm looping through a table to pull out job_id, job_title, job_position, and job_year variables, and then outputting the data into an editable HTML form, but through the loop the names of the text fields stay the same. This becomes problematic when I try to update any job listing after the first, when all the text fields, though echoing different values, are all being labeled in the same way.

 

Can anyone offer some advice on what the best way to get around this would be?

 

Here's my code:

 

    <form action="detail.php?id=<? echo $id; ?>" method="post">

 

<?

while(list($job_id, $job_name, $job_position, $job_year)= mysql_fetch_row($job_result))

{

    echo "Job name: <input type=\"text\" name=\"job_name\" value=\"$job_name\" /><br/>" .

        "Position(s): <input type=\"text\" name=\"job_position\" value=\"$job_position\" /><br/>" .

        "Year(s): <input type=\"text\" name=\"job_year\" value=\"$job_year\" /><hr/>";

}

mysql_free_result($job_result);

?>

 

    <input type="submit" name="job_update" value="Update Job Info"/>

Link to comment
https://forums.phpfreaks.com/topic/134447-issue-with-loops/
Share on other sites

echo "Job name: <input type=\"text\" name=\"job_name[]\" value=\"$job_name\" /><br/>" .
            "Position(s): <input type=\"text\" name=\"job_position[]\" value=\"$job_position\" /><br/>" .
             "Year(s): <input type=\"text\" name=\"job_year[]\" value=\"$job_year\" /><hr/>";

 

This will turn $_POST['job_name']etc etc into arrays containing all submitted values.

Link to comment
https://forums.phpfreaks.com/topic/134447-issue-with-loops/#findComment-699996
Share on other sites

Wouldn't $job_name[] for example return different values per user?

 

Say User A's record pulls up 3 jobs, then the loop will run through each record echoing $job_title, $job_position, etc. into the text fields of my HTML form. However, the labels for the text fields remain the same throughout the loop. So what I end up with looks like:

 

Job name: <input type="text" name="job_title" value="First job" /><br/>

Position(s): <input type="text" name="job_position" value="El Presidente" /><br/>

Year: <input type="text" name="job_year" value="1932" /><hr/>

 

Job name: <input type="text" name="job_title" value="Second job" /><br/>

Position(s): <input type="text" name="job_position" value="Right wing" /><br/>

Year: <input type="text" name="job_year" value="1917" /><hr/>

 

Job name: <input type="text" name="job_title" value="Third job" /><br/>

Position(s): <input type="text" name="job_position" value="Faculty" /><br/>

Year: <input type="text" name="job_year" value="1917" /><hr/>

 

Is there a way I can get the query to count the number of jobs per record, then to list a numerical index next to each text field name ie:

 

<input type="text" name="job_year_<? echo $job_name_count; ?>" value="$job_name" />

 

Or should I be approaching this in another way? Am I misunderstanding what you suggested?

Link to comment
https://forums.phpfreaks.com/topic/134447-issue-with-loops/#findComment-700045
Share on other sites

I think I'm getting closer. Here's my edited code:

 

$job_query = "select * from jobs where student_id = $id";

$job_result = mysql_query($job_query);

 

if ($_POST['job_edit'] != "" and $_SESSION['logged_in']) {

$error_message = "";

$student_id = $id;

$job_name = escape_data($_POST['job_name']);

$job_position = escape_data($_POST['job_position']);

$job_year = escape_data($_POST['job_year']);

 

if (mysql_num_rows($job_result) == 0) {

 

$job_if_query = "insert into jobs (job_name, job_position, job_year, student_id) VALUES ('$job_name', '$job_position', '$job_year', '$student_id')";

 

} else {

 

$job_if_query = "update jobs set job_name = \"$job_name\", job_position = \"$job_position\", job_year = \"$job_year\" where student_id = \"$id\" limit 1";

 

}

 

// No errors? Then write to database

if ($error_message == "") {

$job_if_result = mysql_query($job_if_query);

}

 

include ("header.php");

echo "<h2>Thanks!</h2><p>Student job information has been <strong>updated</strong>.";

echo "<p>" . $job_if_query . "</p>";

include("footer.php");

exit;

}

 

    <form action="detail.php?id=<? echo $id; ?>" method="post">

 

<?

    # while($job = mysql_fetch_array($job_result, MYSQL_NUM))

while(list($job_id, $job_name, $job_position, $job_year)= mysql_fetch_row($job_result))

{

    echo "Club/association name: <input type=\"text\" name=\"job_name[]\" value=\"$job_name\" /><br/>" .

        "Position(s): <input type=\"text\" name=\"job_position[]\" value=\"$job_position\" /><br/>" .

        "Year of participation: <input type=\"text\" name=\"job_year[]\" value=\"$job_year\" /><hr/>";

}

mysql_free_result($job_result);

?>

 

    <input type="submit" name="job_edit" value="Update Club Information"/>

 

This code results in the following output:

update jobs set job_name = "Array", job_position = "Array", job_year = "Array" where user_id = "27" limit 1

 

I found the extract function but I'm not sure if it's what I'm looking for. Sorry, I'm inexperienced.

 

Link to comment
https://forums.phpfreaks.com/topic/134447-issue-with-loops/#findComment-700093
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.