Displaying PHP Functions With HTML/CSS
I have some php and css code that I'm trying to break down into functions. I need some advice on how to approach this.
<?php
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
?>
<div style="width: 100%;">
<div style="float: left;">
<?php echo $info_id; ?>
</div>
<div style="float: left;">
<?php echo $info_title; ?>
</div>
<div style="clear: both;"></div>
</div>
<?php } ?>
This appears as such:
1 One
2 Two
3 Three
4 Four
Below is what I have so far with separating everything into functions:
function get_info()
{
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$result[] = $info_row;
}
mysqli_free_result($info);
return $result;
}
function display_info_id()
{
$result=forum();
foreach ($result as $info_row)
{
$info_id = $info_row['info_id'];
echo $info_id;
}
}
function display_info_title()
{
$r开发者_运维问答esult=forum();
foreach ($result as $info_row)
{
$info_title = $info_row['info_title'];
echo $info_title;
}
}
<?php get_info(); ?>
<div style="width: 100%;">
<div style="float: left;">
<?php display_info_id(); ?>
</div>
<div style="float: left;">
<?php display_info_title(); ?>
</div>
<div style="clear: both;"></div>
</div>
This displays as: 1234 OneTwoThreeFour
I understand why it displays as such, but I don't understand how to get it to show the way I want it to. Any advice would be helpful.
Edit--
Is this at all possible to do with the 3 functions I have? I do not want info_id and info_title in the same function.
When I look at software, such as WordPress, it seems I may have to surround my HTML in some kind of loop, and change the way the functions get info?
You are calling a separate loop for each of your variables. It is kind of unnecessary to split this into multiple functions, since the loop requires that the two variables be displayed together.
How would I handle it? I would store the SQL result into a complete array, then loop over that array to output your HTML. That could be split into two functions, if you wish. Maybe useful if you need to use the results more than once on the page.
I'm using the HEREDOC string syntax to combine the PHP variables, (wrapped in {}
) with the HTML block. This saves you having to jump in and out of PHP via <?php ?>
// This function is fine.
function get_info()
{
$dbc = get_dbc();
$info = mysqli_query($dbc, "SELECT info_id, info_title FROM text") or die("Error: ".mysqli_error($dbc));
while ($info_row = mysqli_fetch_array($info))
{
$result[] = $info_row;
}
mysqli_free_result($info);
return $result;
}
function display_result($result)
{
// Loop over your results and display each
foreach ($result as $r)
{
// Now use the HEREDOC syntax to form your HTML block instead
// of intermingling PHP function calls
echo <<<HTML
<div style="width: 100%;">
<div style="float: left;">
{$r['info_id']}
</div>
<div style="float: left;">
{$r['info_title']}
</div>
<div style="clear: both;"></div>
</div>
HTML;
// There must be no whitespace before or after the HTML; on the previous line!
}
}
Now to retrieve your data and display it:
<?php
$results = get_info();
display_result($results);
?>
You need to refactor your display logic into one loop, instead of two separate loops.
For example:
<?php get_info(); ?>
<div style="width: 100%;">
<div style="float: left;">
<?php display_info_id(); ?>
</div>
<div style="float: left;">
<?php
$result=forum();
foreach ($result as $info_row)
{
$info_id = $info_row['info_id'];
$info_title = $info_row['info_title'];
echo $info_id . " . $info_title . "<br/>\n";
}
?>
</div>
<div style="clear: both;"></div>
</div>
精彩评论