0

If you look at the jsfiddle you'll notice modules in yellow and red. I have all my code working except that I have to manually input the html/php for the modules.

http://jsfiddle.net/W6zYA/1/

My module code is:

 <?php $row = mysql_fetch_row($result); ?>
 <a href='#' data-reveal-id="<?php echo $row[0]; ?>" data-animation="none" >
 <div class="grid_3">
<p id="contexttitle">
 <?php  echo $row[1]; ?>
     <span style="float:right;"> <?php echo $row[3]; ?> </span>
    </p>
    <p id="accesssubmenu">Last Update: <?php echo $row[6]; ?> </p>
 </div>
 </a> 
 <div id="<?php echo $row[0]; ?>" class='reveal-modal'>                 
 <h1> <?php echo $row[1]; ?> </h1>
 <p> <?php echo $row[4]; ?> </p>
 <p4>Last Update:  <?php echo $row[6]; ?> </p4>
 <a class="close-reveal-modal">&#215;</a>
 </div>

What I'm wanting to do is have it create X amount of these modules based on a SQL Query where I count the # of rows in each section (red, yellow, green).

I tried to store the php in the SQL database and after research found out that this is bad practice and shouldn't be done even when using eval() as there will be user input. I also read about PHP templates such as Smarty but wasn't certain if that was the correct solution for me.

If someone can point me in the right direction I'd greatly appreciate it I'm just not sure where to start/what to google.

4
  • Are you looking for a simple for/while loop in PHP, or include? I'm not sure I understand what you need here. Commented Oct 15, 2013 at 20:31
  • I figured I had a difficult time writing exactly what I needed so was afraid I didn't make it clear enough. I'm fairly certain a loop will have to be used. Let's say the red section has 4 items in the it(the database will tell me that) I want to be able to write 4 modules for the red section. If the yellow as 3 I want to be able to write 3 modules for that section. Basically however many rows in my database have the status for that particular color I'll write that many modules. Can I create a loop in php that will take the result of a sql query and input that? Sorry if I'm not making sense. Commented Oct 15, 2013 at 20:35
  • You are making sense now ;). Look at Gavin's answer below and try it. I think you can take it from here. If not check for other questions on SO. Commented Oct 15, 2013 at 20:38
  • How about an up vote? Commented Oct 15, 2013 at 20:39

3 Answers 3

1

mysql_fetch_row only fetches one row. If you have multiple rows you need mysql_fetch_assoc or mysql_fetch_array and use something like a foreach loop to loop through the results. This will output the html as many times as results in the MySQL query.

Sign up to request clarification or add additional context in comments.

1 Comment

Going to read up on this for a bit already upvoted. Will check once I get it working but pretty certain this will get me where I need to be. Thanks again for your help.
0

Try mysql_num_rows($result) that will give you the number of rows returned by the query. I would try to upgrade to PDO or mysql since the mysql_* functions are going to be deprecated in PHP 5.5

http://php.net/manual/en/function.mysql-num-rows.php

1 Comment

The op is using mysql_fetch_row... This will always return 0 if no results found and 1 if any results found.
0

I assume, that you get multiple records from you database? That way you can use a while loop to loop trough your result.

while ($row = mysql_fetch_row($result)) {
 // do you html code here.
}

That should do the trick.

2 Comments

This won't work... mysql_fetch_row fetches a single row. php.net/manual/en/function.mysql-fetch-row.php
This should work: [from documentation] Returns a numerical array that corresponds to the fetched row and boldmoves the internal data pointer ahead.**bold**

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.