2

So I'm making an eCommerce website for uni and everything was fine until I actually tried viewing the site in chrome. Essentially I'm generating a table from MySQL stuff back at uni. The problem I'm having is above where the table should be. Excuse the code being a mess. I'm in between adding things.

<?php $command='select * from products' ; $runCommand=m ysqli_query($connection, $command); if (mysqli_num_rows($runCommand)>0){ echo "
<form id=\ "shoppingcart\" action=\ "cart.php\" method=\ "post\">"; echo "
  <table>"; echo "
    <thead>"; echo "
      <tr>"; echo "
        <th scope=\ "col\">Image</th>"; echo "
        <th scope=\ "col\">Item</th>"; echo "
        <th scope=\ "col\">Qty</th>"; echo "
        <th scope=\ "col\">UpdatedQty</th>"; echo "
        <th scope=\ "col\">Price</th>"; echo "</tr>"; echo "</thead>"; while($ROWVARIABLE= mysqli_fetch_assoc($runCommand)) { //line BROKE goes here //need an if quantity=0 then don't print the bunch we have under here...// echo "
    <tr>"; echo "
      <td>
        <img src='". $ROWVARIABLE["image"]. "' alt='". $ROWVARIABLE["decription"]. "' height='200' '</img></td>"; //this is still one line, don't flip shit. It 's the img with a hover over Decription
			echo "<td>". $ROWVARIABLE["name"]. "</td>"; //Item 
			echo "<td>".newID[$ROWVARIABLE["ID"]]."</td>"; //Quantity
			echo "<td><select name=\"updateQ\" id=\"updateQ\">"; //Updated Quantity
			echo "<option selected=\"selected\">No change</option>";
			echo "<option>1</option>";
			echo "<option>2</option>";
			echo "<option>3</option>";
			echo "<option>4</option>";
			echo "<option>5</option>";
			echo "<option>6</option>";
			echo "<option>7</option>";
			echo "<option>8</option>";
			echo "<option>9</option>";
			echo "</select>";
			echo "</td>";
			//BROKE 2.0
			// Gotta fix this ^^ line to display £Price of all units added(£Price of one unit)
			
		}
	echo "</table>";
	echo "</br>";
	}
	else{
		echo "Table broken, pls stahp!";
	}
	?>

If you run the code snippit it throws a bunch of echos above the table and I have no idea why or how to fix it. I guess this is probably a rookie error but I'm really panicking. The file is saved as .php and I start the file with <!DOCTYPE html>. This is near the top of my file

<?php $connection = mysqli_connect(the stuff I need to connect.); ?> Thought that is relevant.

https://gyazo.com/0dd6a3d7b025133c5b061e6af06f1091

6
  • 2
    Just looking at the color highlighting in the question code gives a quick visual clue as to what's going wrong. Commented Mar 24, 2016 at 15:51
  • The screenshot looks like PHP isn't processing. The code here looks to be formatted incorrectly. Commented Mar 24, 2016 at 15:53
  • Notepad++ ot NetBeans will show you where are the errors. Commented Mar 24, 2016 at 15:53
  • Might be best to edit out the comment swears... Commented Mar 24, 2016 at 15:53
  • 1
    @Henders Had no idea I put swears. My bad. Edited. Commented Mar 24, 2016 at 16:02

1 Answer 1

3

People often run into problems like this when echoing out lots of HTML. One of PHP's strengths is that it is an excellent templating language. You can avoid these types of problems by using that to your advantage. Just write HTML, and use PHP where it is needed to insert dynamic content. This way you won't have to worry about all the various quote escaping, etc. There may be other problems with your code, but this should at least illustrate the general idea of what I'm talking about:

<?php
$command = 'select * from products';
$runCommand = mysqli_query($connection, $command);
if (mysqli_num_rows($runCommand) > 0): ?>
<form id="shoppingcart" action="cart.php" method="post">
    <table>
        <thead>
            <tr>
                <th scope="col">Image</th>
                <th scope="col">Item</th>
                <th scope="col">Qty</th>
                <th scope="col">UpdatedQty</th>
                <th scope="col">Price</th>
            </tr>
        </thead>
        <?php while($ROWVARIABLE = mysqli_fetch_assoc($runCommand)): ?>
        <tr>
            <td>
                <img src="<?= $ROWVARIABLE["image"] ?>"
                     alt="<?= $ROWVARIABLE["decription"] ?>" height="200">
            </td>
            <td><?= $ROWVARIABLE["name"] ?></td>
            <td><?= newID[$ROWVARIABLE["ID"]] ?></td>
            <td>
                <select name="updateQ" id="updateQ">
                    <option selected="selected">No change</option>
                    <option>1</option>
                    <option>2</option>
                    <option>3</option>
                    <option>4</option>
                    <option>5</option>
                    <option>6</option>
                    <option>7</option>
                    <option>8</option>
                    <option>9</option>
                </select>
            </td>
        </tr>
    <?php endwhile; ?>
</table>
</br>
<?php else: ?>
    Table broken, pls stahp!
<?php endif; ?>
Sign up to request clarification or add additional context in comments.

4 Comments

Godlike. Thanks so much. All that shows above the table now is 0): ?> but frankly I can deal with that. Might actually be able to do some real work now. So essentially what you've done is taken all the echos out and the bits that need PHP are in speech marks and PHP tags? So you've kinda written it in html dropping bits of PHP in as apposed to writing it in PHP echoing all the HTML?
Yes. In general, I believe this is a cleaner, easier to write and maintain method of creating views with PHP. Also, as someone mentioned in the comments, using an IDE or a text editor with appropriate syntax highlighting will make these kinds of problems more evident as you work.
dont use short tags most servers have them turned off <?= use <?php instead
@Dave Those are not short open tags. Short open tags are <?, not <?=. <?= is the shortcut syntax for echo, and works regardless of the short_open_tag configuration setting in every supported version of PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.