I’m having a bunch of features table in MySQL DB in my PHP project. I’m fetching through php and creating a table with textboxes named by column name and id of record in photos table using code:
functions.php Code:
function m_html_editPhotos($id) {
$result = "<table class=\"tabelka\" id=\"tbl_photos\"><thead><tr><th>Miniaturka</th><th>Duże zdjęcie</th></tr></thead><tbody>";
$mysqli = m_db_Init();
$qry = "SELECT ID, Img_Min, Img_Nrm FROM tbl_Zdjecie WHERE ID_Grzyb = ?";
if($stmt = $mysqli -> prepare($qry)) {
$stmt -> bind_param("i", $id);
mysqli_stmt_bind_result($stmt, $img_id, $img_min, $img_nrm);
$stmt->execute();
$i =0;
while (mysqli_stmt_fetch($stmt)) {
$i = $i+1;
$result .= "<tr><td><!--<label>Link do miniaturki:<label>--><input type=\"text\" class=\"required\" name=\"photo[$i][min]\" value=$img_min></td><td><!--<label>Link do zdjęcia pełnego rozmiaru:</label>--><input type=\"text\" class=\"required\" name=\"photo[$i][nrm]\" value=$img_nrm></td><td style=\"display:none;\"><input type=\"text\" name=\"photo[$i][id]\" value=$img_id /></td></tr>";
}
$stmt -> close();
}
mysqli_close($mysqli);
$result .= "</tbody></table><div class=\"link\" onclick=\"AddPhotoEditRow();\">Dodaj kolejne zdjęcie</div>";
return $result;
}
Now what I’d like is to edit photos table iterating through each row of generated as above table with textboxes for thumbnail_url (img_min) and full size link (img_nrm)
Additionally I’d like to add new ones to the table from dynamically created rows by function AddPhotoEditRow();
functions.js Code:
function AddPhotoEditRow(){
$('#tbl_photos > tbody:last').append('<tr><td><input type="text" name="photo[0][min]"></td><td><input type="text" name="photo[0][nrm]"></td><td style=\"display:none;\"><input type=\"text\" name=\"photo[0][id]\" value=\"0\" /></td></tr>');
}
edit.php Code:
include 'functions.php';
if(isset($_POST["change"])) m_db_updateAllFeatures($_GET["id"]);
if (m_db_isAdmin("")){
if (!isset($_GET["id"]) || !is_numeric($_GET["id"]))
header("Location: ../index.php");
else {
if (isset($_POST["Nazwa_PL"]))
m_db_UpdateName("PL", $_GET["id"],$_POST["Nazwa_PL"]);
if (isset($_POST["Nazwa_Lac"]))
m_db_UpdateName("Lac", $_GET["id"],$_POST["Nazwa_Lac"]);
render_edit_body();
}
}
I’d like to iterate somehow through photos links and update existing records by parsing textbox names passed through $_POST, additionally would be good to insert new photo links (that with id=0). I’m setting new rows’ id to 0 because I need to distinguish if I’m inserting to table, or updating, right? I’ve assumed that all 0-indexed fields should be added, rest of them should be inserted. I might have my conception wrong, if there is a better way to do full functionality to that table “control” then I’m very open to suggestions.
You’ve got the basics, except
the
0should not be hardcoded. You’re telling php to create an array of ‘photo’ values in the $_POST array, but then you force the index keys to be 0 for all entries, causing later ones to overwrite earlier ones.Keep a count in Javascript as to how many of these fields you’ve inserted, and use that count to increment the index key, e.g.
instead, where
iis the field count. Then it’s a simple matter of:to get that min value for each photo field.