Reputation: 1145
I have a number of table rows, which I choose according to the number of items that I plan to extract from my database.
I don't render the rows automatically, because in some of them I want to have the freedom to include some static elements that do not have a certain uniform (see ABC
, XYZ
in code for example).
For each item (person) I also manually set in the PHP file itself, some other relevant values for his particular row, such as $company[i]
PHP:
<?php
// Choose Relevant people, and turn them into an array
$item_array = array(
'Mike',
'Bill',
'Joe'
);
//implode items, turn into string
$item_implode = join("','", $item_array);
//declare an overall array for result
$person = array();
$personList = array();
$result = $mysqli->query("SELECT available from people_table where available IN ('$item_implode') ORDER BY FIELD (available, '$item_implode');");
if ($result->num_rows > 0) {
$x = 1;
// output data of each row
while($row = $result->fetch_assoc()) {
$person[$x]["available"] = $row['available'];
$x = $x + 1;
}
} else {
echo "0 results";
}
// Manually Set Values:
$hide1=false;
$comment1="hello";
$company1="apple";
$hide2=false;
$comment2="something";
$company2="microsoft";
// And so on...
?>
Since I add another code block for each row, I have to replace in each block the index number, according to the order of the row in the HTML code, which means that in each block, I should CTRL-F the number, and replace it with the following number, which doesn't feel very efficient:
HTML:
// First Row
<?php if ($person[1]["available"]=="yes") { ?>
<tr<?= !$hide1 ? "" : " class=\"hidden\""; ?>>
<td>ABC</td>
<td><?= !$comment1 ? "" : "<div class=\"cell\">" . $comment1 . "</div>"; ?></td>
<td><?= some_function( $company1 ) ?></td>
</tr>
<?php } else { } ?>
// Second Row
<?php if ($person[2]["available"]=="yes") { ?>
<tr<?= !$hide2 ? "" : " class=\"hidden\""; ?>>
<td>XYZ</td>
<td><?= !$comment2 ? "" : "<div class=\"cell\">" . $comment2 . "</div>"; ?></td>
<td><?= some_function( $company2 ) ?></td>
</tr>
<?php } else { } ?>
// and so on
Is there any way to get the indexing number which appears in the end of each variable, to be generated automatically according to the row number in the HTML?
Edit:
Thanks to Andrew Cheong's answer I made an advancement:
while
loop (one line after code start, and another line before code end) to each Table Row, which allows me to indicate index number just once:So it works fine but I think it's not perfect yet, how else shall I modify it?
New Code:
// First Row
<?php if ($person[1]["available"]=="yes") { ?>
<?php $i = 1; while ($i > 0) { ?> /// New Line #1
<tr<?= !$hide[$i] ? "" : " class=\"hidden\""; ?>>
<td>ABC</td>
<td><?= !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
<td><?= some_function( $company[$i] ) ?></td>
</tr>
<?php break; } ?> /// New Line #2
<?php } else { } ?>
// Second Row
<?php if ($person[2]["available"]=="yes") { ?>
<?php $i = 2; while ($i > 0) { ?> /// New Line #1
<tr<?= !$hide[$i] ? "" : " class=\"hidden\""; ?>>
<td>XYZ</td>
<td><?= !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
<td><?= some_function( $company[$i] ) ?></td>
</tr>
<?php break; } ?> /// New Line #2
<?php } else { } ?>
// and so on
Upvotes: 0
Views: 814
Reputation: 30283
You could use actual arrays, still—
// Manually Set Values:
$hide[1]=false;
$comment[1]="hello";
$company[1]="apple";
$hide[2]=false;
$comment[2]="something";
$company[2]="microsoft";
and where they don't apply, simply don't set them, e.g.
$comment[3]="only a comment";
and in your HTML,
<?php
if ($person[1]["available"]=="yes") { ?>
<tr<?= !isset($hide[$i]) || !$hide[$i] ? "" : " class=\"hidden\""; ?>>
<td>ABC</td>
<td><?= !isset($comment[$i]) || !$comment[$i] ? "" : "<div class=\"cell\">" . $comment[$i] . "</div>"; ?></td>
<td><?= if (isset($company[$i])) { some_function( $company[$i] ); } ?></td>
</tr>
<?php } else { } ?>
I didn't do all the work for you, but you can see I've modified it so you can now place the thing in a loop indexing over $i
.
If you don't know how many $i
there will be, then use a while
loop, and get creative, e.g. break
when isset($hide[$i]) && isset($comment[$i]) && isset($company[$i]) == false
.
If you want to hide the comment column altogether, then pull the isset
outside the <td>
so that you don't even created a <td>
in that case.
The way I've wrapped some_function...
in an if( ... )
probably won't work because you're using the short-echo form <?= =>
but I'll let you figure that out.
Re: New Code / Comments
Let me take your new code and modify it a little. I'm going to fix the formatting too so that indentations match up, too:
<?php
$i = 1;
while ($i > 0) {
if ($person[$i]["available"]=="yes") {
?>
<!--
Only add the hidden class if $hide[$i] has been set by someone,
i.e. you, manually, AND it has been set to true.
-->
<tr<?= isset($hide[$i]) && $hide[$i] ? " class=\"hidden\"" : ""; ?>>
<td>ABC</td>
<!--
Same as above, only add the comment div if it has been set, but
instead of what you're doing, I'm going to do what I think you
meant to do: only if there's a non-empty string for a comment.
-->
<td><?= isset($comment[$i]) && $comment[$i] != "" ? "<div class=\"cell\">" . $comment[$i] . "</div>" : ""; ?></td>
<!--
Same thing yet again: only proceed if $company[i] is set and not
an empty string.
-->
<td><?= isset($company[$i]) && $company[$i] != "" ? some_function( $company[$i] ) : "" ?></td>
</tr>
<?php
} // The `else` case is not required.
$i++;
if (!isset($hide[$i]) &&
!isset($comment[$i]) &&
!isset($company[$i]))
{
// Now, this entire thing can be put in a form inside the
// `while`'s condition so that there is no need for this `if`
// statement nor the `break`, but I'm doing it this way to
// make it clearer to you what's going on.
break;
}
}
?>
That's it. No other loops, no other blocks. Now let me answer your questions in your comment:
Of course, no, I didn't mean to use a while
per row. That'd be silly and save you no work. I meant the above, where you need only one row.
In my "don't set" example I meant to show that $hide[3]
and $company[3]
were not set; only $comment[3]
.
As I'm sure some have tried to tell you, your problem wasn't that you just needed to convert numbers to variables. You actually picked the wrong approach, and unfortunately, stubbornly held onto that approach. I know what it's like to feel like, "Yes, I know! I know! But I have a special case! And in my special case, I need it to be this way!" But seasoned programmers know what kinds of special cases merit certain techniques, and it was obvious that your special case had nothing to do with what's called "reflection" or "variable variables," which you were trying to use. What you were missing were some simple isset()
s (and there are probably 10 other ways to do this too, without reflection). isset()
checks if, well, you've set the variable, manually, non-manually, whatever. That means you can now create a loop for each <tr>
and use isset()
on each row's respective variable to see whether you want to do anything with it. If you copy-paste this code to create a new, custom, one-off page, where $hide
and $comment
and $company
are no longer relevant, then guess what: Nothing breaks, because the isset()
s will just ignore them. I hope that makes things clearer, but if it doesn't, no worries, as you continue to program, you'll get accustomed to various patterns, and the knowledge will come automatically.
Upvotes: 1