Jump to content

atpnifb

New Members
  • Posts

    9
  • Joined

  • Last visited

Everything posted by atpnifb

  1. Thanks so much for the advice. You are absolutely right about all that.
  2. Apologies for using the wrong code. I am trying to learn the right way. I have never been taught any of this so I do not know exactly what to do. I have changed $chunk to $chunks in each of the columns and they are all working properly now. Here is the code now: <div class="row"> <?php $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); $codeCount = count($codes); const COLUMNS = 4; $items = $codes; $count = $codeCount; $base = floor($count / COLUMNS); $remainder = $count % COLUMNS; $chunks = []; for ($start = 0; $start < $count; ) { $length = $base + ($remainder-- > 0 ? 1 : 0); $chunks[] = array_slice($items, $start, $length); $start += $length; } ?> <!-- COLUMN 1 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks[0] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 2 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks[1] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 3 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks[2] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 4 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks[3] as $data) { echo $data['vid_code']; } ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div> If you have any advice of what I can look into to change the code to be better or more proper or correct, then I would love to research that. Thank you!
  3. I am so close. I have columns 2, 3, and 4 working perfectly fine. Column 1 is correct but is repeating itself 4 times vertically. Every change I have tried so far throws an error. I tried putting the same code in the first column as the other columns and it everything and says that "$chunk" is undefined. Here is the code when all the columns are "working": (See the first image below with the red checkmarks on it, showing the result of this code where all the columns are "working".) <div class="row"> <?php $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); $codeCount = count($codes); const COLUMNS = 4; $items = $codes; $count = $codeCount; $base = floor($count / COLUMNS); $remainder = $count % COLUMNS; $chunks = []; for ($start = 0; $start < $count; ) { $length = $base + ($remainder-- > 0 ? 1 : 0); $chunks[] = array_slice($items, $start, $length); $start += $length; } ?> <!-- COLUMN 1 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks as $chunk[]) { foreach ($chunk[0] as $data) { echo $data['vid_code']; } } ?> </ul> </div> <!-- COLUMN 2 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[1] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 3 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[2] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 4 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[3] as $data) { echo $data['vid_code']; } ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div> ==================================================================================== Here is the code when I make the first column match the other three that work, and it breaks everything: (See the second image below, showing the result of this code where all the columns are broken.) <div class="row"> <?php $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); $codeCount = count($codes); const COLUMNS = 4; $items = $codes; $count = $codeCount; $base = floor($count / COLUMNS); $remainder = $count % COLUMNS; $chunks = []; for ($start = 0; $start < $count; ) { $length = $base + ($remainder-- > 0 ? 1 : 0); $chunks[] = array_slice($items, $start, $length); $start += $length; } ?> <!-- COLUMN 1 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[0] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 2 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[1] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 3 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[2] as $data) { echo $data['vid_code']; } ?> </ul> </div> <!-- COLUMN 4 --> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunk[3] as $data) { echo $data['vid_code']; } ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div> I am going to bed now but I will resume my research and trial and error method tomorrow. Let me know your thoughts when you have a chance. Thank you!
  4. I truly enjoy learning all types of coding and I have been teaching myself everything throughout the years off and on. I rarely post my own topics for help unless I really do not understand. Most topics are already posted about and answered very well but sometimes you get into something specific and it can be hard to figure out without being able to ask about it directly. This is very encouraging and helpful! I will continue to work on this and post my progress once I get some results. Hints and tips like this is really what I am looking for. I need to think about it and work on my creative and logical problem solving. Copying and pasting an answer does me no good. Even though I used most of your exact code from earlier, I still spent time researching things like "array_slice". That way I can understand WHY you used it and start to see the logic in the code you gave me as an example to start from. Thank you very much again for your time and sharing some of your knowledge on the subject.
  5. Yes that is what I have been doing for hours since your last post. Your code did not show how to split it into the columns so I figured that out. I have also looked up the things I do not understand but I have not been able to get it to work 100 percent yet. I have spent countless hours on learning PHP in the last two weeks. I have been really stuck this past couple days. So I created this account and asked for help. I am not just looking for you to do the work for me, just point me in the right direction. I literally knew nothing about PHP 3 weeks ago, so I still have a long way to go. So far I have only gotten it to show the entire list in each column, instead of equal-ish parts. I have been researching reading searching and trying, but I cannot figure out how to get the values to split between the four columns. I really appreciate your help so far and cannot help if you think I am not doing anything on my end. I am trying really hard whether you believe me or not. If you can continue to help it is greatly appreciated if not I understand and will go elsewhere for advice, not for someone to just give me the exact answer. Here is the current code and attached is the updated HTML view. <div class="row"> <?php function print_r2($val){ echo '<pre>'; print_r($val); echo '</pre>'; } $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } // echo $countOutput; $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); // print_r ($codes); $codeCount = count($codes); // echo $codeCount; // $codes = array_chunk($codes, 4); const COLUMNS = 4; $items = $codes; $count = $codeCount; $base = floor($count / COLUMNS); // 24 $remainder = $count % COLUMNS; // 2 // so $base * COLUMNS + $remainder == $count $chunks = []; for ($start = 0; $start < $count; ) { // the base amount, plus one if there are any remainder to include $length = $base + ($remainder-- > 0 ? 1 : 0); $chunks[] = array_slice($items, $start, $length); $start += $length; } ?> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks as $chunk) { foreach ($chunk as $data) { echo $data['vid_code']; } } ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks as $chunk) { foreach ($chunk as $data) { echo $data['vid_code']; } } ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks as $chunk) { foreach ($chunk as $data) { echo $data['vid_code']; } } ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach ($chunks as $chunk) { foreach ($chunk as $data) { echo $data['vid_code']; } } ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div>
  6. OK so it is working and going in order as expected, but it is showing the full array element in the HTML. How do I get it to only show one thing for each line in the column? It should only show the vid_name or vid_code rather. It should look like the first HTML view image I attached in the original post. I am stuck trying to figure out how to get it to do something like: <?php echo $chunks[0]['vid_code']; ?> But when I try that I get this error: Warning: Undefined array key "vid_code". There are no errors with your original code of: <?php print_r ($chunks[0]); ?>. I attached the Updated HTML view to this post along with the code I just added. Thank you again for your help! <div class="row"> <?php $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } // echo $countOutput; $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); // print_r ($codes); $codeCount = count($codes); // echo $codeCount; // $codes = array_chunk($codes, 4); const COLUMNS = 4; $items = $codes; $count = $codeCount; $base = floor($count / COLUMNS); // 24 $remainder = $count % COLUMNS; // 2 // so $base * COLUMNS + $remainder == $count $chunks = []; for ($start = 0; $start < $count; ) { // the base amount, plus one if there are any remainder to include $length = $base + ($remainder-- > 0 ? 1 : 0); $chunks[] = array_slice($items, $start, $length); $start += $length; } ?> <div class="col-md-3"> <ul class="simple"> <?php print_r ($chunks[0]); ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php print_r ($chunks[1]); ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php print_r ($chunks[2]); ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php print_r ($chunks[3]); ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div>
  7. Got it. I will try this now and see if I can make it work. I will respond back with the results shortly. Thank you!
  8. Thank you so much for your answer! I would prefer the columns be as even as possible, referring to your second example, where the last "row" is incomplete. The first example is totally acceptable though if that is easier to implement. However, being new to all this I am not sure exactly how to write that into my code. I will try to figure it out now but if you can respond with the best way to actually write it in the code I would greatly appreciate that as well!
  9. Hello! New to PHP and this forum! I am trying to get a mySQL database of videos listed into my HTML page in four equal columns. I have done this successfully, however they are being listed in order left to right then down and so. Basically vid01 is in column one then vid02 is in column two same for 3 then 4 then the next row down starts at vid05. I want them in order vertically, i.e. in my test list i have 94 vids ... column one should list vid01 thru vid24...column two should list vid25 thru vid49...column three should list vid50 thru vid74...column four should list vid75 thru vid94 Below is the code I am using so far. Also please see attached images showing the database and the way the HTML looks using the current code. Any help is greatly appreciated! Thank you all so much in advance! <div class="row"> <?php $countQuery = "SELECT COUNT(*) AS count FROM `videos`"; $countResult = mysqli_query($conn, $countQuery); while ($row = mysqli_fetch_assoc($countResult)) { $countOutput = $row['count']; } // echo $countOutput; $codeQuery = $mysqli->query("SELECT * FROM videos WHERE vid_category = 'one' ORDER BY vid_title ASC"); $codes = $codeQuery->fetch_all(MYSQLI_ASSOC); $codes = array_chunk($codes, 4); ?> <div class="col-md-3"> <ul class="simple"> <?php foreach (array_column($codes, 0) as $code): ?> <?php echo $code['vid_code']; ?> <?php endforeach; ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach (array_column($codes, 1) as $code): ?> <?php echo $code['vid_code']; ?> <?php endforeach; ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach (array_column($codes, 2) as $code): ?> <?php echo $code['vid_code']; ?> <?php endforeach; ?> </ul> </div> <div class="col-md-3"> <ul class="simple"> <?php foreach (array_column($codes, 3) as $code): ?> <?php echo $code['vid_code']; ?> <?php endforeach; ?> </ul> <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> </div> </div>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.