php: variable variable names (for loops)

Status
Not open for further replies.

clm100

Ars Legatus Legionis
13,874
Subscriptor
I want to create a new variable name each time a for loop is run. Something to the affect of this (the code below) $i is a for loop, $j is a nested for loop. So the final variable should look like $u1p1 for the first time it runs, $u1p2 for the second time, and then advance all the way to $u2p10
code:<HR>
Code:
$u$ip$j = array($class_id, $row_class["title"], $row_class["teacher"]);
How can I do this?
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
Ah HA! I forgot about concatenate. It isn't quiet working yet, but I've got a start:

$u . $i . "p" . $j = blah;

EDIT: Let me show you the full code. It hangs as mysqld and httpd take up a huge amount of CPU resources.

You specify 2 variables, $uname1 and $uname2. It should run the first for loop twice (once for each user) and embeded for loop 10 times for each period.
code:<HR>
Code:
	for ($i = 1; $i <= 2; $i++) {
		$query . $i = "SELECT * FROM userinfo, scheds WHERE userinfo.uname = '$uname . $i' && userinfo.uname = scheds.uname";
		$results . $i = mysql_query($query . $i,$db); 
		$myrow . $i = @mysql_fetch_array($results . $i); 
		$name . $i = $myrow . $i["fname"] ." ". $myrow . $i["mname"] ." ". $myrow . $i["lname"] ;

		// Turn it into a schedule!
		for ($j = 1; $j <= 10; $j++) {
			$class_id = $myrow . $i["p$j"];
			$query_class = "SELECT teacher, title FROM classes WHERE id ='$class_id'";
			$results_class = mysql_query($query_class,$db);
			$row_class = mysql_fetch_array($results_class);
			$u . $i . "p" . $j = array($class_id, $row_class["title"], $row_class["teacher"]);
		}
	}

[This message was edited by clm100 on February 19, 2002 at 00:44.]
 

Ezran

Wise, Aged Ars Veteran
137
It might be easier (and more standard) to store the values in a multidimensional array indexed by your for loop iterator variables.

code:<HR>
Code:
$bigarray[$u][$i][$j] = array($class_id, $row_class["title"], $row_class["teacher"]);

-- Ezran

Edit: Now that you've posted the rest of the code, I have to say that there's probably a much better way to get the information you're going for. The SQL will let you join the tables together so you don't have to do multiple select statements -- one statement (or one statement for each user) could do all that you're doing with the for loop. I don't have time to study it completely at the moment and give you such a statement, but it also seems that your method could have problems unless every user has exactly 10 class periods returned...

[This message was edited by Ezran on February 19, 2002 at 00:58.]
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
This creates an infinte loop. First it prints "Array" and then the users schedule. Here's an example for a kid who only has 7 classes (it's setup for 8 classes now). It never gets to the second user. The code follows.
Array33020011
Array37120004
Array21280004
Array13240004
Array29785801
Array29620002
Array78350015
Array
Array33020011
Array37120004
Array21280004
Array13240004
Array29785801
Array29620002
Array78350015
Array
code:<HR>
Code:
// Get User 1's info
	for ($i = 1; $i <= 2; $i++) {
		if ($i = 1) {
			$uname = $uname1;
		} else {
			$uname = $uname2;
		}
		$query = "SELECT * FROM userinfo, scheds WHERE userinfo.uname = '$uname' && userinfo.uname = scheds.uname";
		$results = mysql_query($query,$db); 
		$myrow = @mysql_fetch_array($results); 
		${"name" . $i} = $myrow["fname"] ." ". $myrow["mname"] ." ". $myrow["lname"] ;
		// Turn it into a schedule!
		for ($j = 1; $j <= 8; $j++) {
			$period = "p" . $j;
			$class_id = $myrow["$period"];
			$query_class = "SELECT teacher, title FROM classes WHERE id ='$class_id'";
			$results_class = mysql_query($query_class,$db);
			$row_class = mysql_fetch_array($results_class);
			${"u" . $i . "p" . $j} = array($class_id, $row_class["title"], $row_class["teacher"]);
			echo ${"u" . $i . "p" . $j};
			echo "$class_id
";
		}
	}
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
It didn't even occur to me that I might have fucked up the syntax in the if statement. :rolleyes: Oops.

You are a hero among men. :D

EDIT:
Problem now, I need to create another for loop to access the earlier for loop created variables.
code:<HR>
Code:
for ($p = 1; $p <= 8; $p++){
	$user1 = "u1p" . $p;
	$user2 = "u2p" . $p;
is dandy, except that it doesn't actually connect to the variable. (oops) So... how do I get the variable user1 to connect to the array $u1p1, then $u2p1, etc?

EDIT2: Here's the full code. The arrays are being created perfectly.
code:<HR>
Code:
<table border="1" style="text-align: center;">
<tr>
	<td></td>
    <td colspan="3"><?php echo "$name1"; ?>'s Schedule</td>
    <td></td>
    <td colspan="3"><?php echo "$name2"; ?>'s Schedule</td>
</tr>
<tr><td>Period</td> <td>Class number</td> <td>Class name</td> <td>Teacher</td> <td>Match?</td> <td>Class number</td> <td>Class Name</td> <td>Teacher</td></tr>
<?php
for ($p = 1; $p <= 8; $p++){
	$user1 = "u1p" . $p;
	$user2 = "u2p" . $p;
	echo "<tr><td>$p</td>";
	printf("<td>%s</td><td>%s</td><td>$%s</td> \n", $user1["0"], $user1["1"], $user1["2"]);
	if ($user1 == $user2) {
		echo "<td><span style=\"color: red; \">MATCH</span></td>";
	} else {
		echo "<td>no match</td>";
	}
	printf("<td>%s</td><td>%s</td><td>$%s</td></tr> \n", $user2["0"], $user2["1"], $user2["2"]);
}
?>
</table>

[This message was edited by clm100 on February 19, 2002 at 02:10.]

[This message was edited by clm100 on February 19, 2002 at 02:11.]
 

clm100

Ars Legatus Legionis
13,874
Subscriptor
I am so dumb, I am so dumb, D-M-B, D-M-B, er rather, D-U-M-B!

code:<HR>
Code:
printf("<td>%s</td><td>%s</td><td>%s</td>", ${"u1p" . $p}["0"], ${"u1p" . $p}["1"], ${"u1p" . $p}["2"]);

Contrary though it may seem, these aren't just excuses to boost my post count.

EDIT: For the twisted, here is the final code (edited slightly to remove sensitive info
code:<HR>
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> X-check 2 student's Schedules </TITLE>
</HEAD>
<BODY>
<table>
<tr><td>Syntax:</td><td>xcheck.php?uname1=<b>user1</b>&uname2=<b>user2</b></td></tr>
<tr><td>Example:</td><td>xcheck.php?uname1=<ha>&uname2=<ha></td></td>
</table>


<form method=GET action=<?php echo "$PHP_SELF"; ?>>
	<table>
		<tr><td>User 1: <input type="Text" size="9" name="uname1" value="<?php echo $uname1 ?>"></td></tr>
		<tr><td>User 2: <input type="Text" size="9" name="uname2" value="<?php echo $uname2 ?>"></td></tr>
		<tr><td colspan="2"><input type="Submit" name="submit" value="Check these users"></td></tr>
	</table>
</form>
<?php
$db = mysql_connect("no host for you","or a username","and certainly no password") or die("no connection");
mysql_select_db("like I'm tell you the database",$db);

function name($uname) {
	$query = "SELECT * FROM userinfo WHERE uname = '$uname'";
	$results = mysql_query($query); 
	$myrow = @mysql_fetch_array($results); 
	$name = $myrow["fname"] ." ". $myrow["mname"] ." ". $myrow["lname"] ;
	return $name;
}

// Get User 1's info
	for ($i = 1; $i <= 2; $i++) {
		if ($i == 1) {
			$uname = $uname1;
		} else {
			$uname = $uname2;
		}
		$query = "SELECT * FROM userinfo, scheds WHERE userinfo.uname = '$uname' && userinfo.uname = scheds.uname";
		$results = mysql_query($query,$db); 
		$myrow = @mysql_fetch_array($results);
		${"name" . $i} = name($uname);
		// Turn it into a schedule!
		for ($j = 1; $j <= 8; $j++) {
			$period = "p" . $j;
			$class_id = $myrow["$period"];
			$query_class = "SELECT teacher, title FROM classes WHERE id ='$class_id'";
			$results_class = mysql_query($query_class,$db);
			$row_class = mysql_fetch_array($results_class);
			${"u" . $i . "p" . $j} = array($class_id, $row_class["title"], $row_class["teacher"]);
		}
	}
?>

<table border="1" style="text-align: center;">
<tr>
	<td></td>
    <td colspan="3"><?php echo "$name1"; ?>'s Schedule</td>
    <td></td>
    <td colspan="3"><?php echo "$name2"; ?>'s Schedule</td>
</tr>
<tr><td>Period</td> <td>Class number</td> <td>Class name</td> <td>Teacher</td> <td>Match?</td> <td>Class number</td> <td>Class Name</td> <td>Teacher</td></tr>
<?php
$match = 0;
for ($p = 1; $p <= 8; $p++){
	echo "<tr><td>$p</td>";
	printf("<td>%s</td><td>%s</td><td>%s</td>", ${"u1p" . $p}["0"], ${"u1p" . $p}["1"], name(${"u1p" . $p}["2"]));
	if (${"u1p" . $p}["0"] == ${"u2p" . $p}["0"]) {
		echo "<td><span style=\"color: red; \">MATCH</span></td>";
		$match++;
	} else {
		echo "<td>no match</td>";
	}
	printf("<td>%s</td><td>%s</td><td>%s</td>", ${"u2p" . $p}["0"], ${"u2p" . $p}["1"], name(${"u2p" . $p}["2"]));
}
echo "</table> \n";
echo "<P> $name1 has $match classes with $name2 \n";
?>
</BODY>
</HTML>

[This message was edited by clm100 on February 19, 2002 at 02:46.]
 

Ezran

Wise, Aged Ars Veteran
137
Instead of loading everything into variables and doing all that nasty stuff with auto-creating names for 'em and such, you can do the comparisons when you retrieve the table rows.

Here is an example of a revised inner loop:

code:<HR>
Code:
// Get user info here...

// Print out the table header and such here...

$match = 0;
for ($p = 1; $p <= 8; $p++) {
    $query1 = "select teacher, title, id from scheds, classes where scheds.uname = '$uname1' and p$p = id";
    $results1 = mysql_query($query1,$db);
    $row1 = mysql_fetch_array($results1);
  
    $query2 = "select teacher, title, id from scheds, classes where scheds.uname = '$uname2' and p$p = id";
    $results2 = mysql_query($query2,$db);
    $row2 = mysql_fetch_array($results2);
  
    echo "<tr><td>$p</td><td>".$row1['id']."</td><td>".$row1['title']."</td><td>".name($row1['teacher'])."</td>";
    if ($row1['id'] == $row2['id']) {
        echo "<td><span style=\"color: red; \">MATCH</span></td>";
        $match++;
    } else {
        echo "<td>no match</td>";
    }  
    echo "<td>".$row2['id']."</td><td>".$row2['title']."</td><td>".name($row2['teacher'])."</td></tr>";
}

echo "</table> \n";
echo "<P> $name1 has $match classes with $name2 \n";

For any curious others, here is the table information I'm basing the above on, which I've extracted from his above code:
code:<HR>
Code:
create table userinfo (
  uname varchar(20) not null primary key,
  fname varchar(20),
  mname varchar(20),
  lname varchar(20)
)

create table scheds (
  uname varchar(20) not null primary key,
  p1 varchar(8), -- Class ID
  p2 varchar(8),
  p3 varchar(8),
  p4 varchar(8),
  p5 varchar(8),
  p6 varchar(8),
  p7 varchar(8),
  p8 varchar(8)
)

create table classes (
  id      varchar(8) not null primary key,
  teacher varchar(20), -- Teacher's username
  title   varchar(255)
)

-- Ezran
 
Status
Not open for further replies.