Im building this blogg site and finally got everything considering uploading multiple pictures to one news article done.
Now my second step is to remove an article with following pictures to it.
Everything is set in the database so all i need is an ID from the news article.
When i press remove button on a article it shows the ID up in the URL… Im wondering how to get the specifik ID int from the URL and IF there is an smarter approach. It must be OOP and objectoriented apporach.
Code looks like this:
This is the looping out each article and notice $current_id that i put into a button to show in the url when its pressed
public function doOutputArticles($newsList, $pictureList)
{
$result = '';
foreach($newsList as $news)
{
$result .= '<ul>';
$current_id = $news->dbNewsID;
$result .= '<li class="bloggNewsHeader">'.$news->dbHeader.'</li>';
$result .= '<li class="bloggNews">'.$news->dbNews.'</li>';
$result .= '<li class="bloggNewsDate">'.$news->dbNewsDate.'</li>';
foreach($pictureList as $pic)
{
if($pic->dbNewsID == $current_id)
{
$result .= '<li class="bloggNewsPicID">'.$pic->dbPictureID.'</li>';
$result .= '<img class="bloggNewsPic" src="./images/'.$pic->dbPicture.'"alt="some_text">';
}
}
$result .= "
<form id='' method='get'>
<input type='hidden' name='$current_id'/>
<input type='submit' name='removeArticle' value='Ta bort inlägg' />
</form>";
$result .= '</ul>';
}
return "<div id='filesLoaded'>". $result ."</div>";
}
`
After this is done, i press a button to remove specific article and then the ID shows in the URL, now this method is meant to GET the specific INT/NUMBER ID in the URL but HOW?
public function GetSelectedID()
{
$ID = $_GET[self::$articleId];
echo $ID;
if(is_null($ID) || empty($ID))
{
return null;
}
else
{
echo $ID;
return $ID;
}
}
`
Kind regards /Haris
You don’t really need a form, a simple link would do and be clearer. You should be using a different endpoint for deletion rather than looking for “removeArticle”. This would be more OO since it would pave the path towards creating a specific Action Controller for each function rather than having one sprawling endpoint full of conditionals. Then you can have a consistent parameter (ideally within the URL path) of something like “articleId” that you are extracting from the URL. You’d also ideally want to redirect to the listing page upon completion to keep functionality focused in each method and also as a practice to avoid double posting issues (though this example is idempotent so it wouldn’t be overly important).