html - PHP foreach outputs everything four times (mysql, sql, database administration) -
i have code here have feeling can made more efficient. have been building library catalogue database, , looking way store multiple columns (with multiple entries) extracted sql database 1 php array. mean 1 foreach loop required in 'view' code print results out , store them in table. of guys have useful ideas? :)
my code in current state below:
model code: $matches["loandates"] = array(); $matches["loanduedates"] = array(); $matches["itemnames"] = array(); $matches["itemauthors"] = array(); try { // if find item, run query against database list of current loans. $results = $db->prepare(" select il.loan_date, il.loan_duedate, i.item_name, i.item_author itemloan il inner join libraryitem li on il.libraryitem_id = li.libraryitem_id inner join item on li.item_id = i.item_id il.user_id = ? order il.loan_date asc "); $results->bindparam(1,$userid); // puts user id value placeholder is. first value character of placeholder, second value variable want bind placeholder). $results->execute(); } catch (exception $e) { echo "data not retrieved database."; exit; } while ($row = $results->fetch(pdo::fetch_assoc)) { // loop through loans 1 @ time, , add them our matches variable $matches["loandates"][] = $row["loan_date"]; $matches["loanduedates"][] = $row["loan_duedate"]; $matches["itemnames"][] = $row["item_name"]; $matches["itemauthors"][] = $row["item_author"]; } return $matches; }
view code:
<table id="name"> <?php foreach($user["itemnames"] $itemname) { echo "<tr><td id='radiocell'><input type='radio' name='libraryitemid' id='radio'></td><td class='user'>" . $itemname . "</td></tr>"; } ?> </table> <table id="author"> <?php foreach($user["itemauthors"] $itemauthor) { echo "<tr><td class='user'>" . $itemauthor . "</td></tr>"; } ?> </table> <table id="date"> <?php foreach($user["loandates"] $loandate) { $time = strtotime($loandate); $myformatforview = date("l d f y", $time); echo "<tr><td class='user'>" . $myformatforview . "</td></tr>"; } ?> </table> <table id="duedate"> <?php foreach($user["loanduedates"] $loanduedate) { $time = strtotime($loanduedate); $myformatforview = date("l d f y", $time); echo "<tr><td class='user'>" . $myformatforview . "</td></tr>"; } ?> </table>
i want 1 table 1 foreach loop rather 4 tables 4 foreach loops. if meaning.
i thankful if help! heaps in advance!!
regards,
robert, london uk
$matches[] = array("loandates" => $row["loan_date"], "loanduedates" => $row["loan_duedate"], "itemnames" => $row["item_name"], "itemauthors" => $row["item_author"]);
now have 1 array , can display 1 foreach.
foreach($matches $match) { echo $match['loandates'] . '</td><td>' . $match['loan_duedate'] ...
Comments
Post a Comment