1

I have been trying to put HTML inside PHP for a while, but I could never get it to work. Instead I have had to use this

<?php //code in here 
while($record = mysql_fetch_array($myData)){
?>
//HTML code
<?php
}
?>

and it has worked for a while, but now I'm implementing a search function to the page, and I need all HTML to be dynamically generated, or the page will be displayed twice. Therefore, i'm in need of some help. This is the old-new code I have at the moment.

<?php 
$sql = "SELECT * FROM `LISTINGS` ORDER BY YA DESC $limit";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){

//this is what i have manage to get done    
echo '<div class="cards-row">';
    echo '<div class="card-row">';
        echo '<div class="card-row-inner">';
            echo '<div class="card-row-image" data-background-image="http://domains/$record["IMAGENAME"].jpg">';
            echo '</div>';

            echo '<div class="card-row-body">';
                echo '<h2 class="card-row-title">';
                    echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
                    echo '</a>';
                echo '</h2>';
            echo '</div>';

        echo '</div>';
    echo '</div>';
echo '</div>';
?>

Static/dynamic code

    <div class="card-row">
        <div class="card-row-inner">
            <div class="card-row-image" data-background-image="http://www.domain/<?php echo $record['IMAGENAME'];?>.jpg" width="150" height="180" "> 

            </div><!-- /.card-row-image --> 

            <div class="card-row-body">
                <h2 class="card-row-title">
                <a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>"> <?php custom_echo ($record['TITLE'], 80); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?>
                </a>
                </h2>
                <div class="card-row-content"><?php custom_echo ($record['DESCRIPTION'], 250); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?></div><!-- /.card-row-content -->
            </div><!-- /.card-row-body -->

            <div class="card-row-properties">
                <dl>

                        <dd></dd><dt><a href="<?php echo $record['WEBSITE'];?>">Visit Website</a></dt>
                        <dd></dd><dt><a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>">More Info</a></dt>                  
                        <dd>Added</dd><dt><?php echo $record['YA'];?>/<?php echo $record['MU'];?>/<?php echo $record['DU'];?></dt>
                        <dd>Viewed</dd><dt>Visited</dt>

                </dl>
            </div><!-- /.card-row-properties -->
        </div><!-- /.card-row-inner -->
    </div><!-- /.card-row -->
   </div><!-- /.cards-row -->
   <br/>
   <?php 
   }
   ?>

My goal is to put all the code above inside

while($record = mysql_fetch_array($myData)){ 
johnnyRose
  • 7,310
  • 17
  • 40
  • 61
Felipe Lopez
  • 396
  • 3
  • 11
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Mar 29 '16 at 21:17
  • 1
    [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Mar 29 '16 at 21:17
  • 3
    Your goal is to put the code inside the loop? So put it in the loop. I really don't know what you are having trouble with. What is your current output and desired output? How do the 2nd and 3rd code blocks fit together? – Mike Mar 29 '16 at 21:20
  • the way I usually do it is just `echo "my html stuff";`, and escape all double quotes with \" using notepad++ replace all function. `echo "
    test
    ";`
    – Cârnăciov Mar 29 '16 at 21:25
  • As i said, The code in the loop at the moment is not 100% dynamic. Therefore when I put a search field in the page, which searches the database and displays the results, the search output appears on top of the static html, instead of replacing it. The code is in order – Felipe Lopez Mar 29 '16 at 21:27
  • @aron9forever when I do what you said, everything works, but $record['ANYTHING'], in html, . Any suggestions? – Felipe Lopez Mar 29 '16 at 22:11
  • @aron9forever would you mid making that an answer, so I can vote it? – Felipe Lopez Mar 31 '16 at 00:07
  • @FelipeLopez posted, sorry for the delay, haven't visited SO in a while – Cârnăciov Apr 20 '16 at 13:45
  • heredocs are a thing... – I wrestled a bear once. Apr 20 '16 at 13:47

2 Answers2

0

You have an error in line 15

echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'

Change it to:

echo '<a href="http://domain/listing-detail.php?ID='.$record['ID'].'">';

Also, are you saving it in ".php" extension?.

After the last echo, you forget a '}'.

Finally, remove your mysql_ function and use mysqli_ or PDO, if you're on PHP7.

Asfo
  • 413
  • 10
  • 20
0

Although this is not the best way when it comes to readability and maintainability, the way I usually process HTML with PHP is by echoing it.

echo "<div style=\"color:red\">test</div>";

If I need to add PHP content then

$myVar = "test";
echo "<div style=\"color:red\">" . $myVar . "</div>";

And if I already have some HTML code I'd like to escape, I can easily do so using Notepad++'s replace all function by replacing " with \"

Cârnăciov
  • 1,169
  • 1
  • 12
  • 24