arbitter Posted December 29, 2011 Share Posted December 29, 2011 Hi there I never had any php lessons. But I made a site, and now I'm kinda sorta working on a CMS so that someone else can edit the information on the site. Now I was wondering if there was a simple way of displaying all the data from a table, and editing it (not in phpmyadmin). This is my current method; it's rather sloppy but it works like a charm: mysql_select_db('yourdatabase')or die(mysql_error()); $query = mysql_query("SELECT * FROM activiteit ORDER BY datum"); if(isset($_POST['editActiviteit'])){ $id = mysql_real_escape_string(htmlentities($_POST['id'])); }else{ $id = ''; } echo "<table border='1'><tr><th>Begindatum</th><th>Einddatum</th><th>Titel</th><th>Omschrijving</th><th>Zichtbaar?</th><th>Aanpassen</th><th>Verwijderen</th><th>Voorbeeld</th></tr>"; while($row = mysql_fetch_array($query)){ if($id == $row['id']){ if(empty($row['einddatum'])){ $einddatum = "<input type='text' size='2' name='activiteitDagEind' placeholder='dd' />/<input type='text' size='2' maxlength='2' name='activiteitMaandEind' placeholder='mm' />/<input typ='text' size='4' maxlength='4' name='activiteitJaarEind' placeholder='jjjj' />"; }else{ $explode = explode('/',$row['einddatum']); $einddatum = "<input type='text' size='2' name='activiteitDagEind' value='$explode[0]' />/<input type='text' size='2' maxlength='2' name='activiteitMaandEind' value='$explode[1]' />/<input typ='text' size='4' maxlength='4' name='activiteitJaarEind' value='$explode[2]' />"; } $explode2 = explode('/',$row['datum']); $datum = "<input type='text' size='2' name='activiteitDag' value='$explode2[0]' />/<input type='text' size='2' maxlength='2' name='activiteitMaand' value='$explode2[1]' />/<input typ='text' size='4' maxlength='4' name='activiteitJaar' value='$explode2[2]' />"; if($row['actief'] == 0){ $actief = "<select name='actief'><option value='1'>1</option><option value='0' selected='selected'>0</option></select>"; }else{ $actief = "<select name='actief'><option value='1' selected='selected'>1</option><option value='0'>0</option></select>"; } echo "<form action='ward.php' method='post'>"; echo "<tr><td>$datum</td><td>$einddatum</td><td><input type='text' name='activiteitTitel' value='".$row['titel']."' /></td><td><textarea name='activiteitOmschrijving'>".$row['omschrijving']."</textarea></td><td>$actief</td><td><input type='hidden' value='".$row['id']."' name='id' /><input type='submit' value='Slaag op' name='saveActiviteit' /></form></td><td><form action='ward.php' method='post'><input type='hidden' value='".$row['id']."' name='id' /><input type='submit' name='verwijderActiviteit' value='verwijderen' /></form></td><td><input type='button' disabled='disabled' value='Voorbeeld' /></td></tr>"; }else{ echo "<tr><td>".$row['datum']."</td><td>".$row['einddatum']."</td><td>" . $row['titel'] . "</td><td>" . bbcode_format($row['omschrijving']) . "</td><td>" . $row['actief']."</td><td><form action='ward.php?fx=activiteit' method='post'><input type='hidden' value='".$row['id']."' name='id' /><input type='submit' value='Pas aan' name='editActiviteit' /></form></td><td><form action='ward.php' method='post'><input type='hidden' value='".$row['id']."' name='id' /><input type='submit' value='verwijderen' name='verwijderActiviteit' /></form></td><td><input type='button' value='Voorbeeld' onClick=\"parent.location='ward.php?fx=activiteit&voorbeeld=".$row['id']."'\" /></td></tr>"; } } echo "</table><br /><br />"; I just copy pasted all the code but I'll explain it in big lines: First I open a table. I manually enter a row with the headers. Then every result of the query gets shown in a cell. The last cells of each row contain a delete and an edit button. The delete button deletes that row with the hidden id and reloads the page. When the edit button is clicked, it sends a form with the name of the button, here 'editActiviteit', and the id of the entry. And then the page gets reloaded. When the query is then done again, it checks if $_POST['editActiviteit'] is set, and if it is, it displays input fields with the values in the cells instead of pure text, and a save button instead of a edit button. The system works just fine, but it's heaps of work every time I implement it, for activities, users, ... Also I've been told that table's are not the correct way for displaying results (and they also get a little stretched). How should it be displayed? Thanks in advance. 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.