Jump to content

Problem displaying Names from Array


Chezshire

Recommended Posts

Hello,

  I'm very new to PHP, and i think i still count as being 'new' to these forums with only a month or two of reading time. I'm building a game site as I like to do RPGs and I want to learn to use the web for what it has. Currently i'm very stuck, and i think it's a very easy solution, but two weeks of looking and picking and poking at has left me very confused and frustrated (although i did find many solutions to other things so it wasn't a waste ;)

 

PROBLEM: The names in the array, Don't display, with out them Displaying I can't get onto the next problem which is getting the elements that would respond to those items named in the array to display. I'm basing this off of some code i hammered out earlier and thought would be easy to adapt but it's proving to be more difficult then pulling teeth with a broken tweezer (and equally painful too!)

ini_set('error_reporting',E_ALL);

// SQUADRONS

$sql = "SELECT id, name, codename, squadronDB FROM cerebra WHERE approved=\"true\" AND squadronDB IS NOT NULL AND squadronDB != \"\" AND type=\"student\" AND player > 0";
$result = mysql_query($sql);

$squad = array("ALPHA - Alpha Flight (Advisor)",
"ALPHA - Alpha Flight (Field Leader)", "ALPHA - Alpha Flight (Deputy Leader)",
"ALPHA - Alpha Flight", "BETA - Buckaroo Banzias (Advisor)", "BETA - Buckaroo Banzias (Field Leader)", "BETA - Buckaroo Banzias (Deputy Leader)", "BETA - Buckaroo Banzias", "KAPPA - Corsairs (Advisor)", "KAPPA - Corsairs (Field Leader)", "KAPPA - Corsairs (Deputy Leader)", "KAPPA - Corsairs", "DELTA - Paragons (Advisor)", "DELTA - Paragons (Field Leader)", "DELTA - Paragons (Deputy Leader)", "DELTA - Paragons", "EPSILON - Excaliber (Advisor)", "EPSILON - Excaliber (Field Leader)", "EPSILON - Excaliber (Deputy Leader)", "EPSILON - Excaliber", "OMEGA - Omega Gang (Advisor)", "OMEGA - Omega Gang (Field Leader)", "OMEGA - Omega Gang (Deputy Leader)", "OMEGA - Omega Gang");

$squadders = array();

WHILE ($squadders = mysql_fetch_assoc($result)) {
$tempSquad = explode("^", $squadders["squadronDB"]);

foreach ($tempSquad as $thisSquad) {

$key = array_search($thisSquad, $squad);

if ($key) {
if (!isset($squadders[$key])) { $squadders[$key] = array(); }
array_push($squadders[$key], $squadders["id"]);
} // end if in squad list

} // end foreach

} // end WHILE

$col = 0;

echo "<br>\n<h2><font color='#A5B9D5'>TRAINING SQUADRONS</font></h2>\n";

echo "<hr color='#050F1D' size='5'>\n";

echo "<table cellpadding='0'><tr>";
for ($i =0; $i < count($squad); $i++) {

if (isset($squadders[$i])) {
if ($col == 0) { echo "<td valign='top'>"; }
echo "<p><b>$squad[$i]</b><br>\n";
foreach ($squadders[$i] as $thisSquadders) {
$safeName = preg_replace("/\"/", """, getSquad($thisSquadders));
$realName = getRealName($thisSquadders);
if ($realName == "(Unrevealed)") { $realName = $safeName; }
echo "<span class='fineprint'><a href=\"/cerebra/display.php?id=$thisSquadders\" title=\"$safeName\">$realName</a></span><br>\n"; }
echo "</p>\n";
$col++;
if ($col == 6) { echo "</td><td width='25'></td>\n"; $col=0; }
} // end if any students in this squad

} // end for

IF ($col < 4) { echo "</td>"; }

echo "</tr></table>\n";

?>

 

 

Here is the code that I'm trying to adapt from:

// SCHOOL CLUBS

$sql = "SELECT id, schoolclubs FROM cerebra WHERE approved=\"true\" AND schoolclubs IS NOT NULL AND schoolclubs != \"\" AND type=\"student\" AND player > 0";
$result = mysql_query($sql);

$clubs = array("Archery", "Art", "Computer", "Chess", "Debate", "Drama", "GBLT(S)", "Independant Study", "Language", "Martial Arts", "Music", "Science", "Sports");

$clubbers = array();

WHILE ($clubber = mysql_fetch_assoc($result)) {
$tempClubs = explode("^", $clubber["schoolclubs"]);

foreach ($tempClubs as $thisClub) {

$key = array_search($thisClub, $clubs);

if ($key) {
if (!isset($clubbers[$key])) { $clubbers[$key] = array(); }
array_push($clubbers[$key], $clubber["id"]);
} // end if in club list

} // end foreach

} // end WHILE

$col = 0;

echo "<br>\n<h2><font color='#A5B9D5'>SCHOOL CLUBS</font></h2>\n";

echo "<hr color='#050F1D' size='5'>\n";

echo "<table cellpadding='0'><tr>";
for ($i =0; $i < count($clubs); $i++) {

if (isset($clubbers[$i])) {
if ($col == 0) { echo "<td valign='top'>"; }
echo "<p><b>$clubs[$i]</b><br>\n";
foreach ($clubbers[$i] as $thisClubber) {
$safeName = preg_replace("/\"/", """, getChar($thisClubber));
$realName = getRealName($thisClubber);
if ($realName == "(Unrevealed)") { $realName = $safeName; }
echo "<span class='fineprint'><a href=\"/cerebra/display.php?id=$thisClubber\" title=\"$safeName\">$realName</a></span><br>\n"; }
echo "</p>\n";
$col++;
if ($col == 4) { echo "</td><td width='25'></td>\n"; $col=0; }
} // end if any students in this club

} // end for

IF ($col < 4) { echo "</td>"; }

echo "</tr></table>\n";

 

 

Here is the pages full code:

<?php
include("../functions.php");
include("../header.php");
?>

<h1 class='spaceafter'>The Xavier Institute for the Gifted</h1>

<hr color='#050F1D' size='5'>

<p><?php

$subSections["institute"] = array("courses","extracuriculum","schoolRules","schedules","squadrons");
$subSectionLabels["institute"] = array("Courses","Extracuriculum","Rules, Policies & Regulations","Schedules","Squadrons");

FOREACH ($subSections["institute"] as $key=>$thisSec) {
echo "<a href='$thisSec.php'>" . $subSectionLabels["institute"][$key] . "</a>";
if ($key != 4) { echo " &#8226; "; }
} // end FOREACH

?></p>

<hr color='#050F1D' size='5'>


<DIV ALIGN="CENTER">
<table width='600' cellpadding='6' cellspacing='0' cellborder='0'>

<TR>
<TD COLSPAN="3"><a href="javascript:openimage('/institute/floorplan.php',800,625,true)" title="Click for an interactive floor plan of the mansion" onmouseover="window.status='Click for an interactive floor plan of the mansion'"><img src="../images/outside.jpg" alt="Click Here" width="100%"></a></TD></TR>

<TR>
<TD width='124' align='center'><p><a href='/institute/brochure.pdf' title="Download an informative brochure about Xavier's"><img src="/images/brochure.jpg" alt="Brochure" width="124" height="250" border="0"><br>
<span class='fineprint'>Get PDF Brochure</span></a></p>
</TD>

<TD>
<p>Here at the Xavier Institute for the Gifted, we strive every day to provide exceptional young people with the specialized education and training they need. The School was founded in 1989 by Charles Francis Xavier, who has served an uninterrupted 15-year term as headmaster until his untimely death in of phnemonia in the summer of 2006. From its humble beginnings as an exclusive academy for a handful of special students, it has grown into a full-fledged center of learning: a college preparatory program recognized as one of the finest, and possibly the most exclusive in all the world. Admittance is strictly on a case-by-case basis.</p>
</TD>

<TD width='124' align='center'><p><a href='/images/XPGmanhattan.jpg' title="Download a map of Manhattan's amazing sites and sounds!"><img src="/images/XPGmanhattan_sm.jpg" alt="Brochure" width="124" height="250" border="0"><span class='fineprint'>See Manhattan</span></a> 

</TR>

<TR>
<TD COLSPAN="3" align="left">

<?php
// STUDENT ADVISORS

$sql = "SELECT id, advisor FROM cerebra WHERE approved=\"true\" AND advisor IS NOT NULL AND advisor != \"\" AND type=\"student\" AND player > 0 ORDER BY advisor";
$result = mysql_query($sql);

echo "<br>\n<h2><font color='#A5B9D5'>STUDENT ADVISORS</font></h2>\n";

echo "<hr color='#050F1D' size='5'>\n";

echo "<table cellpadding='0'><tr>";

$currentAdvisor = "";
$col = 0;

WHILE ($advisee = mysql_fetch_assoc($result)) {

$thisAdvisorID = $advisee["advisor"];
$advisorRealName = getRealName($thisAdvisorID);
$advisorCodeName = getChar($thisAdvisorID);
$advisorCodeName = preg_replace("/\"/", """, $advisorCodeName);
$advisorRealName = preg_replace("/\"/", """, $advisorRealName);
if ($advisorRealName == "(Unrevealed)") { $advisorRealName = $advisorCodeName; }

$thisID = $advisee["id"];
$realName = getRealName($thisID);
$codeName = getChar($thisID);
$codeName = preg_replace("/\"/", """, $codeName);
$realName = preg_replace("/\"/", """, $realName);
if ($realName == "(Unrevealed)") { $realName = $codeName; }


if ($thisAdvisorID != $currentAdvisor) {
if ($col == 5) { echo "</td><td width='25'></td>\n"; $col=0; }
if ($col == 0) { echo "<td valign='top'>"; }
echo "\n\n<!-- COL: $col -->\n\n";
if ($currentAdvisor && $col > 0) { echo "</p>\n\n"; }
echo "<p><a href=\"/cerebra/display.php?id=$thisAdvisorID\" title=\"$advisorCodeName\"><font color='white'>$advisorRealName</font></a><br>\n";
$col++;
} // end if new advisor subheader

echo "<span class='fineprint'><a href=\"/cerebra/display.php?id=$thisID\" title=\"$codeName\">$realName</a></span><br>\n";

$currentAdvisor = $thisAdvisorID;

} // end WHILE

IF ($col < 5) { echo "</td>"; }

echo "</tr></table>\n";




// SCHOOL CLUBS

$sql = "SELECT id, schoolclubs FROM cerebra WHERE approved=\"true\" AND schoolclubs IS NOT NULL AND schoolclubs != \"\" AND type=\"student\" AND player > 0";
$result = mysql_query($sql);

$clubs = array("Archery", "Art", "Computer", "Chess", "Debate", "Drama", "GBLT(S)", "Independant Study", "Language", "Martial Arts", "Music", "Science", "Sports");

$clubbers = array();

WHILE ($clubber = mysql_fetch_assoc($result)) {
$tempClubs = explode("^", $clubber["schoolclubs"]);

foreach ($tempClubs as $thisClub) {

$key = array_search($thisClub, $clubs);

if ($key) {
if (!isset($clubbers[$key])) { $clubbers[$key] = array(); }
array_push($clubbers[$key], $clubber["id"]);
} // end if in club list

} // end foreach

} // end WHILE

$col = 0;

echo "<br>\n<h2><font color='#A5B9D5'>SCHOOL CLUBS</font></h2>\n";

echo "<hr color='#050F1D' size='5'>\n";

echo "<table cellpadding='0'><tr>";
for ($i =0; $i < count($clubs); $i++) {

if (isset($clubbers[$i])) {
if ($col == 0) { echo "<td valign='top'>"; }
echo "<p><b>$clubs[$i]</b><br>\n";
foreach ($clubbers[$i] as $thisClubber) {
$safeName = preg_replace("/\"/", """, getChar($thisClubber));
$realName = getRealName($thisClubber);
if ($realName == "(Unrevealed)") { $realName = $safeName; }
echo "<span class='fineprint'><a href=\"/cerebra/display.php?id=$thisClubber\" title=\"$safeName\">$realName</a></span><br>\n"; }
echo "</p>\n";
$col++;
if ($col == 4) { echo "</td><td width='25'></td>\n"; $col=0; }
} // end if any students in this club

} // end for

IF ($col < 4) { echo "</td>"; }

echo "</tr></table>\n";





ini_set('error_reporting',E_ALL);

// SQUADRONS

$sql = "SELECT id, name, codename, squadronDB FROM cerebra WHERE approved=\"true\" AND squadronDB IS NOT NULL AND squadronDB != \"\" AND type=\"student\" AND player > 0";
$result = mysql_query($sql);

$squad = array("ALPHA - Alpha Flight (Advisor)",
"ALPHA - Alpha Flight (Field Leader)", "ALPHA - Alpha Flight (Deputy Leader)",
"ALPHA - Alpha Flight", "BETA - Buckaroo Banzias (Advisor)", "BETA - Buckaroo Banzias (Field Leader)", "BETA - Buckaroo Banzias (Deputy Leader)", "BETA - Buckaroo Banzias", "KAPPA - Corsairs (Advisor)", "KAPPA - Corsairs (Field Leader)", "KAPPA - Corsairs (Deputy Leader)", "KAPPA - Corsairs", "DELTA - Paragons (Advisor)", "DELTA - Paragons (Field Leader)", "DELTA - Paragons (Deputy Leader)", "DELTA - Paragons", "EPSILON - Excaliber (Advisor)", "EPSILON - Excaliber (Field Leader)", "EPSILON - Excaliber (Deputy Leader)", "EPSILON - Excaliber", "OMEGA - Omega Gang (Advisor)", "OMEGA - Omega Gang (Field Leader)", "OMEGA - Omega Gang (Deputy Leader)", "OMEGA - Omega Gang");

$squadders = array();

WHILE ($squadders = mysql_fetch_assoc($result)) {
$tempSquad = explode("^", $squadders["squadronDB"]);

foreach ($tempSquad as $thisSquad) {

$key = array_search($thisSquad, $squad);

if ($key) {
if (!isset($squadders[$key])) { $squadders[$key] = array(); }
array_push($squadders[$key], $squadders["id"]);
} // end if in squad list

} // end foreach

} // end WHILE

$col = 0;

echo "<br>\n<h2><font color='#A5B9D5'>TRAINING SQUADRONS</font></h2>\n";

echo "<hr color='#050F1D' size='5'>\n";

echo "<table cellpadding='0'><tr>";
for ($i =0; $i < count($squad); $i++) {

if (isset($squadders[$i])) {
if ($col == 0) { echo "<td valign='top'>"; }
echo "<p><b>$squad[$i]</b><br>\n";
foreach ($squadders[$i] as $thisSquadders) {
$safeName = preg_replace("/\"/", """, getSquad($thisSquadders));
$realName = getRealName($thisSquadders);
if ($realName == "(Unrevealed)") { $realName = $safeName; }
echo "<span class='fineprint'><a href=\"/cerebra/display.php?id=$thisSquadders\" title=\"$safeName\">$realName</a></span><br>\n"; }
echo "</p>\n";
$col++;
if ($col == 6) { echo "</td><td width='25'></td>\n"; $col=0; }
} // end if any students in this squad

} // end for

IF ($col < 4) { echo "</td>"; }

echo "</tr></table>\n";



?>

</TD>
</TR>

</TABLE>

</DIV>


<?php 
include("../mods.php"); 
include("../footer.php"); 
?>

 

 

Any help is very much appreciated. I can't figure out what i'm doing wrong and i'm infinitely confused.

 

Link to comment
Share on other sites

Hmm, your code looks a little confused. Perhaps if you explain what you're actually trying to achieve, we might be able to provide a neat solution.

 

It looks to me like you just need an extra condition to your WHERE clause, rather than cycling through the results and checking against an array. But as I say, it's a bit difficult to tell without knowing what you're trying to do.

Link to comment
Share on other sites

Hmm, your code looks a little confused. Perhaps if you explain what you're actually trying to achieve, we might be able to provide a neat solution.

 

It looks to me like you just need an extra condition to your WHERE clause, rather than cycling through the results and checking against an array. But as I say, it's a bit difficult to tell without knowing what you're trying to do.

 

Ok - thanks GingerRobot (great name!)

My goal is simple -- I want to be able to display all the names of the teams, and who is on each team. Teams are assigned and displayed using the Alpha through Omega thing. If you go to this URL you can see an example of what I'm trying to do: http://www.xpg.us/institute/institute.php

 

As you can see, We have student advisors, and then all the people each person advises. Then we have school clubs, and all the people in each club. I want to display each squadron, and all the people in each squadron.

Any help is uber appreciated -- i've honestly no idea of what i'm doing, that said i've built a nice homebrewed RPG site populated with losts of nice people. And i'm just trying to make something nice for people to play in while i myself try to understand this whole amazing internet thing and everything that can be done with it.

 

So any help is uber welcomed!

Link to comment
Share on other sites

The simple answer for me to give is that your data appears to be rather hopelessly stored. If you're really having to go to the lengths of the above to generate a list of people in a club, then you've got issues with your database structure.

 

Normally, the way you'd do the above is with three tables:

 

Table: clubs

Fields: clubid, clubname

 

Table: clubparticipants

Fields: clubid, userid

 

Table: users

Fields: userid, firstname, lastname etc

 

Edit: Note that for other thing such as your Squadrons, you'd need two further tables: squadrons and squadronaparticipants and their structure would be similar to clubs an clubpartipants

 

You would then be able to select everything from your clubparticipants table, and use the clubid and userid to grab the club name and person's name from the clubs and users table respectively. You can then order by clubid, and only show this when it changes. Something like this:

 

<?php
$sql = "SELECT p.clubid,p.userid,c.clubname,u.firstname,u.lastname FROM clubparticipants as p, clubs as c, users as u WHERE u.userid=p.userid AND c.clubid=p.clubid ORDER BY p.clubid";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$prevclubid = '';
while($row = mysql_fetch_assoc($result)){
    $clubid = $row['clubid'];
    if($clubid != $prevclubid){//the club has changed
        echo $row['clubname'].'<br />';
        $prevclubid = $clubid;
    }
    echo $row['firstname'].' '.$row['lastname'].'<br />';
}
?>

 

The above is far cleaner and much easier to maintain than your current code. To take one example, what happens with your current code if another club is created? You'll have to modify your code, rather than just adding to the database. Not good.

 

All that said, if you're just looking for a quick and easy solution then there are at least some differences between the code you're adapting from and what you have currently. For example, these lines:

 

WHILE ($squadders = mysql_fetch_assoc($result)) {
$tempSquad = explode("^", $squadders["squadronDB"]);

 

Should be:

 

WHILE ($squadder = mysql_fetch_assoc($result)) {
$tempSquad = explode("^", $squadder["squadronDB"]);

 

Note that there is no 's' on $squadder, just as there is no 's' on $clubber in those lines you're adapting from. I wouldn't guarantee that changing that will fix your problem (though it could be argued that your problem would not really be fixed until you changed your database structure), since i've not followed your code through and I don't know your database structure/

Link to comment
Share on other sites

hi Chezshire....

 

i think there is some problem in queries:

 

$sql = "SELECT id, name, codename, squadronDB FROM cerebra WHERE approved=\"true\" AND squadronDB IS NOT NULL AND squadronDB != \"\" AND type=\"student\" AND player > 0";

 

it should be:

 

$sql = "SELECT id, name, codename, squadronDB FROM cerebra WHERE approved='true' AND squadronDB IS NOT NULL AND squadronDB != '' AND type='student' AND player > 0";

 

 

Link to comment
Share on other sites

Thank you to the both of you for your offerings and ideas. I'm trying to examine them and understand them. I'm a complete newb and an old folkie newb at that.

 

I explored the following code, and got the following error which happens with the first line.

 

<?php
$sql = "SELECT p.clubid,p.userid,c.clubname,u.firstname,u.lastname FROM clubparticipants as p, clubs as c, users as u WHERE u.userid=p.userid AND c.clubid=p.clubid ORDER BY p.clubid";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$prevclubid = '';
while($row = mysql_fetch_assoc($result)){
    $clubid = $row['clubid'];
    if($clubid != $prevclubid){//the club has changed
        echo $row['clubname'].'<br />';
        $prevclubid = $clubid;
    }
    echo $row['firstname'].' '.$row['lastname'].'<br />';
}
?>

 

the Error:

unexpected T_CONSTANT_ENCAPSED_STRING

 

I'm now googling and trying to understand the T string issue - they always confuse me as i don't really understand what it is.

 

Link to comment
Share on other sites

Well, there isn't an error in the code i provided, so i can only assume it's generated elsewhere in the code. However, what i gave there was more of an example of the way you should be doing it. It wouldn't work with your current database structure.

 

As for understanding the error, this might help you.

Link to comment
Share on other sites

Well, there isn't an error in the code i provided, so i can only assume it's generated elsewhere in the code. However, what i gave there was more of an example of the way you should be doing it. It wouldn't work with your current database structure.

 

As for understanding the error, this might help you.

 

Thank you for the clarification. I'm very much the novice, so i tried making a page with the code above and nothing else to see what would happen and I received an error. I'm not disputing what you are saying, just stating what I did to try to understand what you were saying - I've never seen PHP code that has things like 'p.' or 'c.' or 'u.' as your does. It looks really clean and far far far beyond me at this moment. Hopefully i'll get there though - I'm trying. Thanks for the suggestion of a place to look - I will be doing that tonight. And thanks to Singla also who's suggestion I will be exploring.

 

Thanks again to both of you. Your very kind people and I really genuinely appreciate your help and suggestions!

Chez

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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