d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Php Echo Advice And Other Good Practices > Please And Thank You. ^_^
Add Reply New Topic New Poll
Member
Posts: 1,208
Joined: Aug 1 2013
Gold: 50.00
Nov 19 2013 01:14am
Let me start by saying I'm extremely new to HTML5, CSS, JS, MySQL, and PHP. I've been working with PHP and MySQL for all of a week now with no proper instruction, so I'm sorry if my code offends you. :(

First Concern:
Which is proper usage of echo in this case? *see code below* I googled and basically the answer was to try to separate the markup and php as much as possible, but I couldn't get it working properly within HTML tags, and I wasn't sure if it's still good practice when you're using this many PHP references.

Near complete separation of markup and PHP. Mostly readable, but constant reuse of <?php ?> seems a bit cluttered.
Code
...
$x = 0;
while($row = mysqli_fetch_array($result))
{
$x++;
?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<?php
echo '<a data-toggle="collapse" data-parent="#accordion" href="#collapse' . $x . '">';
?>
<div class="row">
<font face="verdana">
<div class="col-xs-6">
<?php
echo $row['Price'] . '. ' . $row['Name'];
?>
</div>
<div class="col-xs-4">
<?php
echo $row['Type'];
?>
</div>
<div class="col-xs-2">
<?php
echo '*rating*';
?>
</div>
</font>
</div>
<?php
echo '</a>';
?>
</h4>
</div>
<?php
echo '<div id="collapse' . $x . '" class="panel-collapse collapse">';
?>
<div class="panel-body">
<div class="row">
<div class="col-xs-6">
<?php
echo $row['Bio']
?>
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-8">
<?php
echo $row['Address'] . '<br>' .
$row['City'] . ', ' . $row['State'] . ' ' . $row['ZipCode'];
?>
</div>
<div class="col-xs-4">
<?php
echo $row['Phone #'] . '<br>Price(1-5): ' . $row['Price'];
?>
</div>
<div class="form-group row">
<div class="col-xs-4"></div>
<div class="col-xs-6">
<br>
<label class="control-label">Add to Calendar</label>
<div class="input-group date" id="dp3" data-date="11/18/2013" data-date-format="mm/dd/yyyy">
<input class="form-control" type="text" readonly="" value="11/18/2013">
<span class="input-group-addon">
Add
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
echo '</div>';
?>
</div>
<?php
}
if($x == 0) {
echo 'No results found. :(';
}



One big echo block. Inefficient (?) but readable. Sadly no IDE smart suggestions or color coding (at least not with NetBeans). ~yes I know it's slightly different than above, sorry~
Code
...
$x = 0;
while($row = mysqli_fetch_array($result))
{
$x++;
echo '
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse' . $x . '">
<div class="row">
<font face="verdana">
<div class="col-xs-6">' . $row['Price'] . '. ' . $row['Name'] . '</div>
<div class="col-xs-4">' . $row['Type'] . '</div>
<div class="col-xs-2">' . '*rating*' . '</div>
</font>
</div>
</a>
</h4>
</div>
<div id="collapse' . $x . '" class="panel-collapse collapse">
<div class="panel-body">
<div class="col-xs-6">' . $row['Bio'] . '</div>
<div class="col-xs-4">' . $row['Address'] . '<br>' .
$row['City'] . ', ' . $row['State'] . ' ' . $row['ZipCode'] . '</div>
<div class="col-xs-2">' . $row['Phone #'] . '
<br>
<form role="form">
<div class="checkbox">
<label>
<input type="checkbox">Add to favorites
</label>
</div>
</form>
</div>
</div>
</div>
</div>
';
}
if($x == 0) {
echo 'No results found. :(';
}




Second Concern:
If you actually bothered to skim through my crap, please let me know any bad practices you see that can be reconciled! Thank you

Third Concern:
Any other standard conventions I should be aware of / things that bother you smart peoples when the less-than-smart-peoples don't do it properly / etc. <3


I like learning to do things the proper way, that way there's no unlearning of bad habits.
Member
Posts: 29,723
Joined: Jun 11 2007
Gold: 279.52
Nov 19 2013 01:41am
i dont like the one big echo block, its actually more confusing to sort through and change because of all the string concatenations.
but ur up top can be condensed as well

For Example, you have it written like this
Code

<?php
echo $row['Type'];
?>


you can save urself 2 lines by wiriting it like this:
Code
<?php echo $row['Type']; ?>


other than that the only other thing i would suggest is make better use of ur html tags
for example, i dont know the rest of your code, but that h4 should only be used AFTER h1,h2,h3 have been used. if u just want to style it a certain way, use a <p></p> tag.
also i would need to see this styled and actually like on jfiddle or live, but i think u might be over div'ing

This post was edited by AkuuZ on Nov 19 2013 01:42am
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Nov 19 2013 02:24am
You can use short tags if you have it enabled: http://php.net/manual/en/language.basic-syntax.phptags.php

I would do it like this:
Code
$index = 0;
while($row = mysqli_fetch_array($result)){

$index++;
$price = $row['Price'];
$name = $row['Name'];
$type = $row['Type'];
$bio = $row['Bio'];
$address = $row['Address'];
$city = $row['City'];
$state = $row['State'];
$zipcode = $row['ZipCode'];
$phone = $row['Phone #'];
$price = $row['Price'];
$rating = '*rating*';

?>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse<?= $index ?>">
<div class="row">
<font face="verdana">
<div class="col-xs-6">
<?= $price ?>. <?= $name ?>
</div>
<div class="col-xs-4">
<?= $type ?>
</div>
<div class="col-xs-2">
<?= $rating ?>
</div>
</font>
</div>
</a>
</h4>
</div>
<div id="collapse<?= $index ?>" class="panel-collapse collapse">
<div class="panel-body">
<div class="row">
<div class="col-xs-6">
<?= $bio ?>
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-8">
<?= $address ?>
<br />
<?= $city ?>, <?= $state ?> <?= $zipcode ?>
</div>
<div class="col-xs-4">
<?= $phone ?>
<br />
Price(1-5): <?= $price ?>
</div>
<div class="form-group row">
<div class="col-xs-4"></div>
<div class="col-xs-6">
<br>
<label class="control-label">Add to Calendar</label>
<div class="input-group date" id="dp3" data-date="11/18/2013" data-date-format="mm/dd/yyyy">
<input class="form-control" type="text" readonly="" value="11/18/2013">
<span class="input-group-addon">
Add
</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
}


You don't have to assign variables in the beginning ofcourse, it's just my preference to do so.

You can also use a template structure: http://programmers.stackexchange.com/questions/159529/how-to-structure-template-system-using-plain-php

This post was edited by Eagl3s1ght on Nov 19 2013 02:28am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll