I am still trying to finish fixing my details page and I need one more piece to fix it.
I start with this query.
NOTE:recordID actually comes in from the previous page by clicking an item. Also, name, img, item_code, and type_id are fields in my table not outside sources.
$recordID = $_GET['recordID']; $query_master_details = "SELECT * FROM master_list WHERE master_list.master_id = $recordID"; $master_details = mysqli_query($conn, $query_master_details) or die(mysqli_error()); $row_master_details = mysqli_fetch_assoc($master_details); $totalrows_master_details = mysqli_num_rows($master_details);
Now, I have created this to determine how to display the details about said item:
<div class="container2"> <div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div> <?php $crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = $row_master_details.length ORDER BY RAND() LIMIT 1"; $result = mysqli_query($conn, $crafted); $crafted = array(); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $crafted[] = $row; } } if ($row_master_details['type_id'] > 3) {?> <p><strong>Code 1</strong></p> <p><?php echo $crafted['name']; ?></p> <p><img src="img/<?php echo $crafted['img']; ?>" /></p> <p><?php echo $crafted['item_code']; ?></p> <br><br> <p><strong>Code 2</strong></p> <p><?php echo $row_master_details['name']; ?></p> <p><img src="img/<?php echo $row_master_details['img']; ?>" /></p> <p><?php echo $row_master_details['item_code']; ?></p> <p><?php echo $row_master_details['length']; ?> Characters</p> <?php }else { ?> <p><?php echo $row_master_details['name']; ?></p> <p><img src="img/<?php echo $row_master_details['img']; ?>" /></p> <p><?php echo $row_master_details['item_code']; ?></p> <p><?php echo $row_master_details['length']; ?> Characters</p> <?php mysqli_free_result($master_details); ?> <?php } ?> <!-- end .container2 --></div>
To explain what is happening here:
This looks at the item that was clicked on the previous page and finds the information about it. If it has a type_id of 4 or higher, I need it to do the following:
- Look at the “length” of the current item.
- Select all the items from the master_list that has a type_id of 1, 2, or 3 and a matching length to the first one.
- Choose 1 random match.
- Output the “name, img, and item_code in the same fashion as the first one.
If the item selected originally has a type_id of less than 4, it just posts the original information. This part works. The part from the second query does not.
I have a feeling I need another $_GET[], but not exactly sure how to go about it. The recordID get was attached to the image used as a link.
Can anyone help me to query this so that I get what I am wanting?
Here is a pic of what I am trying to do to help make more sense:
This was done by manually choosing the item to match and putting them together. I want a random item to display.
Here is what the current code looks like. This is the same thing I get with MiK’s answer:
Advertisement
Answer
I finally managed to fix this issue with a combination of tweaking and the answer from MiK.
Here is the final code I ended up with:
<div class="container2"> <div class="category"><h2><?php echo $row_master_details['name']; ?></h2></div> <?php $crafted = "SELECT * FROM `master_list` WHERE `type_id` <= 3 AND `length` = ". $row_master_details['length']." ORDER BY RAND() LIMIT 1"; $result = mysqli_query($conn, $crafted); $citem = array(); if (mysqli_num_rows($result) > 0) { while($row = mysqli_fetch_assoc($result)) { $citem[] = $row; } } if ($row_master_details['type_id'] > 3) {?> <p><strong>Code 1</strong></p> <p><?php echo $citem[0]['name']; ?></p> <p><img src="img/<?php echo $citem[0]['img']; ?>" /></p> <p><?php echo $citem[0]['item_code']; ?></p> <br><br> <p><strong>Code 2</strong></p> <p><?php echo $row_master_details['name']; ?></p> <p><img src="img/<?php echo $row_master_details['img']; ?>" /></p> <p><?php echo $row_master_details['item_code']; ?></p> <p><?php echo $row_master_details['length']; ?> Characters</p> <?php }else { ?> <p><?php echo $row_master_details['name']; ?></p> <p><img src="img/<?php echo $row_master_details['img']; ?>" /></p> <p><?php echo $row_master_details['item_code']; ?></p> <p><?php echo $row_master_details['length']; ?> Characters</p> <?php mysqli_free_result($master_details); ?> <?php } ?> <p><h4>(Need a different crafted item? Refresh the page!)</h4></p> <!-- end .container2 --></div>
The answer provided by MiK fixed the query, but the results were not showing because I had an array inside the array and had to call the inner array. Also, I had to fix the array name so it wasn’t the same as the query.