Reputation: 111
I have an html table that lists the various folders in a directory using PHP
. The folder names are an abbreviation of the full name of a company. I'm calling MySQL
in order to access the database that holds the key between the company abbreviation ($comp_id
) and the company name ($short_name
). Everything works except when I try to check the $comp_id
with the name of the directory (which is the same), in order to print the short_name
into the table. Here's what I have:
$myDirectory = opendir(".");
$blacklist = array(".", "..");
while(false !== ($entryName = readdir($myDirectory))) {
if (!in_array($entryName, $blacklist)) {
$dirArray[] = $entryName;
}
}
closedir($myDirectory);
$indexCount = count($dirArray);
if ($stmt = $con->prepare($query)) {
$stmt->execute();
$stmt->bind_result($comp_id, $short_name);
while ($stmt->fetch()) {
//printf("%s, %s\n", $comp_id, $short_name);
//echo("<br>");
}
$stmt->close();
}
echo ("<TABLE border=1 cellpadding=5 cellspacing=0 class= whitelinks>\n");
echo ("<TR><TH>CheckBox</TH><th>File Name</th><th>Company Name</th></TR>\n");
for ($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != ".") {
echo("<TR><TD><input type=\"checkbox\" name=\"comp[]\" value=\"$dirArray[$index]\"</td>");
echo("<td>");
echo("<a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
if ($comp_id = $dirArray[$index]){
echo("<td>");
echo($short_name);
echo("</td>");
}
echo("</TR>\n");
}
}
echo("</TABLE>\n");
It's echoing the last possible $short_name
and not the corresponding company name of the directory.
I'm new to php
, so this is probably an obvious and rookie mistake.
Upvotes: 0
Views: 70
Reputation: 1112
//make an associative array to hold values
$longNames=array();
if ($stmt = $con->prepare($query)) {
$stmt->execute();
$stmt->bind_result($comp_id, $short_name);
while ($stmt->fetch()) {
//printf("%s, %s\n", $comp_id, $short_name);
//echo("<br>");
//in query, load array using comp_id as key
$longNames[$comp_id]=$short_name;
}
$stmt->close();
}
at the bottom, you have the same $comp_id inside of your loop, because you named the folders using those ids, right?
So in that last for loop,
$comp_id = $dirArray[$index];
$short_name= $lonhNames[$comp_id];
echo "$comp_id , $short_name <br />\r\n";
so the complete code would be something like this...
$myDirectory = opendir(".");
$blacklist = array(".", "..");
while(false !== ($entryName = readdir($myDirectory))) {
if (!in_array($entryName, $blacklist)) {
$dirArray[] = $entryName;
}
}
closedir($myDirectory);
$indexCount = count($dirArray);
//new array here for matching short/long names
$longNames=array();
if ($stmt = $con->prepare($query)) {
$stmt->execute();
$stmt->bind_result($comp_id, $short_name);
while ($stmt->fetch()) {
//printf("%s, %s\n", $comp_id, $short_name);
//echo("<br>");
//in query, load array using comp_id as key
$longNames[$comp_id]=$short_name;
}
$stmt->close();
}
echo ("<TABLE border=1 cellpadding=5 cellspacing=0 class= whitelinks>\n");
echo ("<TR><TH>CheckBox</TH><th>File Name</th><th>Company Name</th></TR>\n");
for ($index=0; $index < $indexCount; $index++) {
if (substr("$dirArray[$index]", 0, 1) != ".") {
echo("<TR><TD><input type=\"checkbox\" name=\"comp[]\" value=\"$dirArray[$index]\"</td>");
echo("<td>");
echo("<a href=\"$dirArray[$index]\">$dirArray[$index]</a></td>");
echo("<td>");
echo($longNames[$dirArray[$index]]);
echo("</td>");
echo("</TR>\n");
}
}
echo("</TABLE>\n");
Upvotes: 1
Reputation: 1559
Your if
statement is using an assignment operator when I believe you are tyring to use a comparison operator.
Try changing
if ($comp_id = $dirArray[$index])
To:
if ($comp_id == $dirArray[$index])
Upvotes: 0
Reputation: 3
You have this
if ($comp_id = $dirArray[$index]){
And it need to be this
if ($comp_id == $dirArray[$index]){
You are missing one =
Upvotes: 0