SkyRanger Posted January 17, 2013 Share Posted January 17, 2013 Not sure what is going on, I cannot get the data to display <?php // CONNECT TO THE DATABASE $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT * FROM `gallery_category` WHERE `photo_owner`='NameHere'"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); $totalCount = $result->num_rows; $colMax = 10; $colcount = ceil($totalCount / $colMax); $counter = 0; while($row = $result->fetch_assoc()) { echo "<tr>"; for($i = 0; $i < $colCount; $i++) { echo "<td>".$row['category_name']."</td>"; } echo "</tr>"; } ?> </table> Can somebody please have a look over, I have been staring at this code for over an hour trying to find problem, And yes Jessica before you say anything i did echo the query and it works. It is something between $totalCount and ?> that is stumping me but I just can't see it. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2013 Share Posted January 17, 2013 And yes Jessica before you say anything i did echo the query and it works. . . . and I think you are missing the bigger point of what Jessica has told you in the past. You need to learn to debug your code. When you encounter an error, just 'staring' at your code is going to be a hit or miss activity on being able to spot the problem. Many times the problem is that a variable or return value does not contain what you think it does. That is why it is important to echo things to the page so you can verify what the values are to determine if they make sense or not. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 17, 2013 Share Posted January 17, 2013 during develop is always a good idea to include this 2 lines at the very beginning (after the opening php tag) of your code (or define it in your php.ini) // Define how to display/report errors ini_set("display_errors", "1"); error_reporting(-1); it should help you with the debugging process. Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2013 Share Posted January 17, 2013 Looking over your code, I think I see the problem. And, you could have done a couple of very simple things to identify the problem yourself. Let me explain the steps I would have taken to identify the problem. You stated that you could not get the data to display. My first instinct would be that the query failed. But, you have error handling in place to know if the query failed so that is probably not it. Although I might put a step in just to echo a "Query succeeded" just to verify. After I have confirmed that the query isn't failing, my next assumption would be that no records were returned. Maybe the value 'NameHere' isn't in that field or the wrong field was used. So, I would do a simple test of echoing the count of records to the page: echo "records returned: " . $result->num_rows; If that comes back as zero, then I know the problem is the query and/or the assumed values in the DB. If the count is greater than zero I would verify that the loop is actually running on those records. Sometimes there is logic in the loop do echo this or that. Maybe the logic is faulty and the loop is running but producing no output. In those cases you could put a simple echo at the beginning of the loop to ensure that it is in fact running. In this case, however, you don't have any such logic and it will always produce output. The problem is that (aside from the values in the result set) the only thing being output in the loop is HTML tags. If the values you are trying to display are empty you would see no "apparent" output in the page. I would bet that if you did a view source in your browser you are going to see those table HTML tags. The problem is that the values you are expecting are not included in the table. And why is that? Well, it is not apparent from the code if there is a field called 'category_name' or not - so that could be the cause. If you are only using one field from the table, then specify it in the field list of the SELECT statement - don't use '*'. But, even so, that code would not do what you think it should. If it worked it would be repeating the results of each record multiple times. Quote Link to comment Share on other sites More sharing options...
mikosiko Posted January 17, 2013 Share Posted January 17, 2013 all good points...... and more basic than that.... knowing the basics of the programming language is a must: http://php.net/manual/en/language.variables.basics.php Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2013 Share Posted January 17, 2013 Give this a try. It has the correct logic to output the results as you want them. But, may not have data if you don't find the previous error <?php // CONNECT TO THE DATABASE $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT category_name FROM `gallery_category` WHERE `photo_owner`='NameHere'"; $result = $mysqli->query($query) or die($mysqli->error.__LINE__); //Define number of columns to use $maxColumns = 10; $count = 0; //Var to count records $categoryList = ''; //Var to hold HTML output while($row = $result->fetch_assoc()) { $count++; //Increment count //Open new row if first record in row if($maxColumns % $count == 1) { $categoryList .= "<tr>\n"; } //Output current record $categoryList .= "<td>{$row['category_name']}</td>\n"; //Close row if last record in row if($maxColumns % $count == 0) { $categoryList .= "</tr>\n"; } } //Close last row if needed if($maxColumns % $count != 0) { $categoryList .= "</tr>\n"; } ?> <table> <?php echo $categoryList; ?> </table> Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 17, 2013 Author Share Posted January 17, 2013 (edited) Thank you Psycho, it is greatly appreciated. I finally narrowed down my code, and found the error: for($i = 0; $i < $colCount; $i++) { With the error message: Notice: Undefined variable: colCount And I do need to figure out what I did wrong. Thank you for all your help all of you. It is greatly appreciated. Reference the Namehere is actually $userlogged to specify the user that is logged in. Edited January 17, 2013 by SkyRanger Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 17, 2013 Author Share Posted January 17, 2013 Awesome script Psycho but I found a problem. Doing some research by googleing the different parts of the scripts. But my tables are coming out like this: <table width="600" border="1"> <td>Family</td> </tr> <td>Kids</td> </tr> <tr> <td>Christmas</td> <td>Halloween</td> </tr> <td>Kid Birthday</td> <td>Trip</td> </tr> </table> Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2013 Share Posted January 17, 2013 I reversed the $maxColumns and $count variables in the modulus operation. These two lines if($maxColumns % $count == 1) if($maxColumns % $count == 0) Should be changed to this if($count % $maxColumns == 1) if($count % $maxColumns == 0) Quote Link to comment Share on other sites More sharing options...
SkyRanger Posted January 17, 2013 Author Share Posted January 17, 2013 You are a life saver, thank you very much Psycho. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.