Skip to content

Instantly share code, notes, and snippets.

@SilasRodrigues19
Created March 21, 2023 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SilasRodrigues19/db9ae2ff64fefb8a27ad5852a0466891 to your computer and use it in GitHub Desktop.
Save SilasRodrigues19/db9ae2ff64fefb8a27ad5852a0466891 to your computer and use it in GitHub Desktop.
Exemplo de validação de informação no banco, antes de cadastrar
<?php
$autoload['helper'] = array('url', 'html', 'file', 'basic'); // basic nome do helper que eu criei pra exibir mensagem de erro
?>
<?php
function notify($title, $msg, $type, $width)
{
$_SESSION['title'] = $title;
$_SESSION['msg'] = $msg;
$_SESSION['type'] = $type;
$_SESSION['width'] = $width;
}
function showMessage()
{
if(isset($_SESSION['title'])) {
echo alertBox($_SESSION['title'], $_SESSION['msg'], $_SESSION['type'], $_SESSION['width']);
unset($_SESSION['title']);
unset($_SESSION['msg']);
unset($_SESSION['type']);
unset($_SESSION['width']);
}
}
function alertBox($title, $msg, $type, $width)
{
switch( $type ) {
case 'erro':
case 'error':
case 'danger':
$type = 'alert-danger';
$icon = 'icon-park-solid:close-one';
$title = 'Error!';
break;
case 'ok':
case 'success':
$type = 'alert-success';
$icon = 'akar-icons:circle-check-fill';
$title = 'Success!';
break;
case 'aviso':
case 'warning':
$type = 'alert-warning';
$icon = 'ph:warning-circle-fill';
$title = 'Warning!';
break;
case 'info':
$type = 'alert-info';
$icon = 'akar-icons:info-fill';
$title = 'Info';
break;
default:
$type = 'alert-default';
$icon = 'mdi mdi-alert-circle mr-2';
$title = 'Warning!';
}
$title = '';
$box = "<div class=\"col-md-6 py-4 my-2 d-flex justify-content-center w-100 showMessage\">
<span class=\"$width text-center alert $type\" role=\"alert\">
<span class=\"iconify mb-1\" data-icon=\"$icon\"></span> <strong> $title </strong> $msg
</span>
</div>";
return $box;
}
<?php
public function insertResource($dados)
{
$verify = "SELECT resource_link FROM resource WHERE resource_link = '".$dados['resource_link']."'";
$execute = $this->db->query($verify);
if($execute->num_rows() > 0) {
notify('', 'Este link já foi cadastrado', 'warning', 'w-50');
return false;
} else {
$insert = "INSERT INTO resource (resource_name, resource_description, resource_link)
VALUES ('{$dados['resource_name']}', " . $this->db->escape($dados['resource_description']) . ", '{$dados['resource_link']}')";
$execute = $this->db->query($insert);
if($execute) {
$insert = "INSERT INTO resource_category (`category_id`, `resource_id`) VALUES ({$dados['resource_category']}, {$dados['resource_id']})";
$execute = $this->db->query($insert);
return true;
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment