Jump to content

Loop with existing variables


alfman05302001

Recommended Posts

So have this code
[code]
$chiefquery = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='Chief' ";
$result = mysql_query($chiefquery) or die("Internal Error: " . mysql_error());
$chief = mysql_fetch_assoc($result);
$chiefemail = $chief["Email"];
$chiefFirst = $chief["FirstName"];
$chiefLast = $chief["LastName"];

$VCPquery = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='VCP' ";
$result = mysql_query($VCPquery) or die("Internal Error: " . mysql_error());
$VCP = mysql_fetch_assoc($result);
$VCPemail = $VCP["Email"];
$VCPFirst = $VCP["FirstName"];
$VCPLast = $VCP["LastName"];
[/code]
ect. ect. ect.

And this code repeats for each position where in general its $(position)query $(position)email ect.  I was hoping to condense this code into a loop without changing the variables since every single page on my website links to them.  The main reason I want to put the effort into creating the loop is this page I am also working on http://oalodge43.org/cont-test.php where the last line of the table allows you to create a new position.  I am not even sure if what I want to do is possible (although it seems like something that would be somewhat common), or even what would be good keywords to google.  I am not opposed to doing research on my own, but I am just not sure where to even begin with this one.  Thanks for your help

Jonathan

Link to comment
https://forums.phpfreaks.com/topic/27188-loop-with-existing-variables/
Share on other sites

I would think it would be more efficient to grab all the values from the db that you want in one query then manipulate that data in php to display it. But, without seeing your code it's hard to say.

But, to answer your question directly I see two options.

1) Create a function where you pass it the position you are looking for, and to make it easy, put the values into a nested array:

[code]<?php
function getPosition($position) {
  $query = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='$position' ";
  $result = mysql_query($query) or die("Internal Error: " . mysql_error());
  $row = mysql_fetch_assoc($result);

  $positionData['email'] = $row['Email'];
  $positionData['fname'] = $row['FirstName'];
  $positionData['lname'] = $row['LastName'];

  return $positionData;
}

$chiefData = getPosition("Chief");
?>[/code]

2) You could put in a loop and assign the values to a nested array like this:

[code]<?php
$positions = array("Chief", "VCP", "Something");
$positionData = array();

foreach ($positions as $position) {
  $query = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='$position' ";
  $result = mysql_query($query) or die("Internal Error: " . mysql_error());
  $row = mysql_fetch_assoc($result);

  $positionData[$position]['email'] = $row['Email'];
  $positionData[$position]['fname'] = $row['FirstName'];
  $positionData[$position]['lname'] = $row['LastName'];
}
?>[/code]
Modification to the code would be inevitable because of the manner in which you currently have your values set. Setting individual variables such as $chiefemail, $chiefFirst, $chiefLast, etc. for each entity is not good programming form. Since each entity (Chief, VCP, etc) has the same three fields and because you are returning those values from a database, it would make better sense to keep the values in an array.

I gave you two options to help streamline your code to make it more effective and be less prone to errors. You can use them or not.

In the first example, when you want to get the data for an entity you just need to set a variable such as I showed at the very end. In that example you would have all three values in the new array called [b]$chiefData[/b]. In fact I would change that function to just assign the array returned from the db query to the new array w/o changing the key names to increase efficiency - like this:
[code]<?php
function getPosition($position) {
  $query = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='$position' ";
  $result = mysql_query($query) or die("Internal Error: " . mysql_error());
  return mysql_fetch_assoc($result);
}

$chiefData = getPosition("Chief");
echo "The Chief's email is " . $chiefData['Email'];
?>[/code]

In the 2nd example, I just created a loop that will get the values for each entity in the $positions array. Just enter all the positions in that array and all the values will be retrieved from the database and returned in an array. Again, in hindsight I would change the loop to just use the key names returned from the query.
[code]<?php
$positions = array("Chief", "VCP", "Something");
$positionData = array();

foreach ($positions as $position) {
  $query = "SELECT * FROM oalodge_plst1.officer_contact WHERE Position='$position' ";
  $result = mysql_query($query) or die("Internal Error: " . mysql_error());

  $positionData[$position] = mysql_fetch_assoc($result);
}

echo "The Chief's email is " . $chiefData['Chief']['Email'];
?>[/code]

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.