AlexTed Posted July 21, 2021 Share Posted July 21, 2021 (edited) ciao a tutti ragazzi grazie a questo forum riesco sempre a risolvere i problemi che riscontro, ma questa volta ho bisogno del mio aiuto perchè non riesco a trovare la soluzione a questo mio piccolo problema. sto creando un modulo dove in un'altra tabella mi deve salvare il nome e l'id della categoria da me selezionata, riesco a salvare solo l'id ma non il nome potete aiutarmi per favore a capire come fare ? questa è una parte del modulo dove al submit mi salva (come detto in precedenza l'id) public function renderAddForm() { $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Impostazione'), ), 'input' => array( array( 'type' => 'categories', 'label' => $this->l('Seleziona Categoria'), 'name' => 'name', 'tree' => array( 'id' => 'category', 'selected_categories' => array((int)Configuration::get('category')), 'use_search' => true, 'use_checkbox' => false, 'required' => true ) ), array( 'type' => 'file_lang', 'label' => $this->l('Seleziona Immagine'), 'name' => 'image', 'required' => true ), ), 'submit' => array( 'title' => $this->l('Save'), 'class' => 'btn btn-default pull-right' ) ); grazie Edited July 21, 2021 by AlexTed (see edit history) Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 @AlexTed Ciao, questo è il modo standard per salvare l'unico ID. Puoi eseguire una query con questa tabella con ID categoria category piace mytable.id_category = ps_category_land.id_category in questo modo puoi trovare facilmente il nome della categoria dalla tabella delle lingue delle categorie Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 (edited) 18 minutes ago, SmartDataSoft said: @AlexTed Ciao, questo è il modo standard per salvare l'unico ID. Puoi eseguire una query con questa tabella con ID categoria category piace mytable.id_category = ps_category_land.id_category in questo modo puoi trovare facilmente il nome della categoria dalla tabella delle lingue delle categorie molto gentile per il suo intervento, mi perdoni ma non ho capito la sua risposta, puoi per favore farmi un esempio pratico ? grazie edit: tipo come questo esempio ? public static function getSpecificCategory($id_category) { $name = Db::getInstance()->executeS( 'SELECT name FROM '._DB_PREFIX_.'category_lang WHERE clients.name = ps_category_lang.id_category' ); var_dump($name); return $name; } Edited July 21, 2021 by AlexTed (see edit history) Link to comment Share on other sites More sharing options...
ziobudda Posted July 21, 2021 Share Posted July 21, 2021 Non devi mai salvare il nome della categoria (o del prodotto, o di altro), ma sempre solo il suo ID perchè quello non muta. Al momento di dover visualizzare i dati (o di salvarli su file o altro) allora recuperi i "nomi" che ti servono. Fai conto di aver scritto "Abbiggliamento" al posto di "Abbigliamento". Se tu salvassi anche il nome della categoria dovresti andare a correggere tutte le entry in cui hai salvato "Abbiggliamento", mentre salvando solo l'ID basta che correggi l'unico Abbiggliamento sbagliato. M. Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 (edited) SELECT * FROM `prrs_category` INNER JOIN` prrs_category_lang` ON prrs_category . id_category = prrs_category_lang . id_category Edited July 21, 2021 by SmartDataSoft (see edit history) Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 (edited) SELECT * FROM `mytable_category` INNER JOIN` mytable_category_category_lang` ON prrs_category . id_category = prrs_category_lang . id_category Edited July 21, 2021 by SmartDataSoft (see edit history) Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 7 minutes ago, ziobudda said: Non devi mai salvare il nome della categoria (o del prodotto, o di altro), ma sempre solo il suo ID perchè quello non muta. Al momento di dover visualizzare i dati (o di salvarli su file o altro) allora recuperi i "nomi" che ti servono. Fai conto di aver scritto "Abbiggliamento" al posto di "Abbigliamento". Se tu salvassi anche il nome della categoria dovresti andare a correggere tutte le entry in cui hai salvato "Abbiggliamento", mentre salvando solo l'ID basta che correggi l'unico Abbiggliamento sbagliato. M. grazie per il tuo intervento e per la spiegazione, quello che a me serve è questo, ti faccio un esempio 1 ho una tabella che si chiama (esempio) clients dove salvo id della categoria ed immagine 2 da un file tpl devo dirgli che se id della categoria clients è uguale a id_category della tabella category_lang visualizza il nome è possibile fare questo ? Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 Questo è possibile quando ti unisci internamente a due tabelle se i dati corrispondono, quindi porterà il nome della categoria altrimenti Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 riesco a vederlo tramite db con la select da lei fornita, ma non riesco a farlo visualizzare nel file tpl Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 Devi eseguire una query all'interno del tuo modulo e quindi inviare il valore nel tuo file modello qualcosa di simile a $sql = " SELECT * FROM `mytable_category` INNER JOIN` mytable_category_category_lang` ON prrs_category . id_category = prrs_category_lang . id_category "; $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $this->context->smarty->assign(array( 'categories' => $results )); grazie Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 10 minutes ago, SmartDataSoft said: Devi eseguire una query all'interno del tuo modulo e quindi inviare il valore nel tuo file modello qualcosa di simile a $sql = " SELECT * FROM `mytable_category` INNER JOIN` mytable_category_category_lang` ON prrs_category . id_category = prrs_category_lang . id_category "; $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $this->context->smarty->assign(array( 'categories' => $results )); grazie non voglio approfittare della sua gentilezza e che non saprei dove collocare questo codice, sono uno studente e mi diletto con prestashop per capire come funziona il modulo che creo è solo per uso scolastico se mi passa il termine, allego il codice da me utilizzato e se lei è così gentile da dirmi dove inserire il codice e come richiamarlo dal file tpl le sarei grato. posso inserire il codice ? Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 Se inserisci il tuo codice, possiamo testarlo e fornirti i dettagli. Oppure puoi caricare il file zip del tuo modulo Grazie Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 (edited) <?php if (!defined('_PS_VERSION_')) { exit; } include ('classes/ClientManager.php'); /** * Class DisplayClient * * @author luigi */ class DisplayClient extends Module { /** * Length of the accepted input in description. */ const DESCRIPTION_CHARACTER_LENGTH = 255; /** * Length of the accepted input in title. */ const TITLE_CHARACTER_LENGTH = 50; /** * Used to combine multiple values to display one value. */ private $output = null; /** * @var ClientManager */ private $clientManager; /** * DisplayClient constructor. * * @param ClientManager $clientManager * * @author luigi */ public function __construct(ClientManager $clientManager) { $this->name = 'displayclient'; $this->tab = 'others'; $this->version = '1.0'; $this->author = 'Luigi'; $this->need_instance = 1; $this->ps_versions_compliancy = array('min' => '1.7.1', 'max' => _PS_VERSION_); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Display client\'s'); $this->description = $this->l('Allows to display the list of client\'s logo in the homepage.'); $this->clientManager = $clientManager; } /** * Allow module to be install. * * @throws PrestaShopDatabaseException * @throws PrestaShopException * @author luigi * * @return bool */ public function install() { $createTable = include_once ($this->getLocalPath().'sql/createTable.php'); return ( !parent::install() OR !$createTable OR !$this->registerHook('displayHome') ) ? false : true; } /** * Uninstall the module installed. * * @author luigi * * @return bool */ public function uninstall() { $dropTable = include_once ($this->getLocalPath().'sql/dropTable.php'); Configuration::deleteByName('title'); Configuration::deleteByName('description'); return (!parent::uninstall() OR !$dropTable) ? false : true; } /** * This automatically display the template of list of clients in the homepage. * * @throws PrestaShopDatabaseException * @author luigi * * @return string */ public function hookDisplayHome() { $this->getValues(); return $this->display(__FILE__, 'client.tpl'); } /** * This allow user to configure some setting in the back office.ss * * @throws PrestaShopDatabaseException * @throws PrestaShopException * @author luigi * * @return string|null */ public function getContent() { $this->getValues(); $form = $this->renderAddForm(); $id = (int)Tools::getValue('id'); if (Tools::isSubmit('submit')) { if ($this->postValidation()) { $this->output .= $this->postProcess(); $this->output .= $this->renderList(); } else { $this->output .= $form; } } elseif ( Tools::isSubmit('addClient') || Tools::isSubmit('editClient') ) { $this->output = $form; } elseif (Tools::isSubmit('deleteClient')) { $this->deleteClient($id); } elseif (Tools::isSubmit('viewClient')) { $this->showClient($id); } elseif (Tools::isSubmit('submitText')) { if ($this->postValidation()) { Configuration::updateValue('title', (string)Tools::getValue('title')); Configuration::updateValue('description', (string)Tools::getValue('description')); Tools::redirectAdmin( $this->context->link->getAdminLink('AdminModules', true) . '&conf=4&configure=' . $this->name ); } else { $this->output .= $this->renderForm(); $this->output .= $this->renderList(); } } else { $this->output .= $this->renderForm(); $this->output .= $this->renderList(); } return $this->output; } /** * Check the validation of every fields. * * @throws PrestaShopDatabaseException * @author luigi * * @return bool */ public function postValidation() { $errors = array(); if (Tools::isSubmit('submit')) { if (empty((string)Tools::getValue('name'))) { $errors[] = $this->l('Please type something in the client name field.'); } if (Tools::strlen((string)Tools::getValue('name')) > self::TITLE_CHARACTER_LENGTH) { $errors[] = $this->l('The name must be at least 50 character only.'); } if ((int)Tools::getValue('id')) { if ( (string)Tools::getValue('name') == $this->getClient((int)Tools::getValue('id'))[0]['name'] ) { $this->output = true; } else { if ($this->isClientExist((string)Tools::getValue('name')) == true) { $errors[] = $this->l('You have entered an name that already exist.'); } } if (is_uploaded_file($_FILES['image']['tmp_name'])) { if ($error = ImageManager::validateUpload($_FILES['image'])) { $errors[] = $this->l($error); } } } else { if ($this->isClientExist((string)Tools::getValue('name')) == true) { $errors[] = $this->l('You have entered an name that already exist.'); } if (is_uploaded_file($_FILES['image']['tmp_name'])) { if ($error = ImageManager::validateUpload($_FILES['image'])) { $errors[] = $this->l($error); } } else { $errors[] = $this->l('Please upload image.'); } } } elseif (Tools::isSubmit('submitText')) { if (empty((string)Tools::getValue('title'))) { $errors[] = $this->l('Please type something in the title field.'); } if ((string)Tools::strlen(Tools::getValue('title')) > self::TITLE_CHARACTER_LENGTH) { $errors[] = $this->l('The title must be at least 50 character only.'); } if (empty((string)Tools::getValue('description'))) { $errors[] = $this->l('Please type something in the description field.'); } if ((string)Tools::strlen(Tools::getValue('description')) > self::DESCRIPTION_CHARACTER_LENGTH) { $errors[] = $this->l('The description must be at least 255 character only.'); } } if (count($errors)) { $this->output = $this->displayError(implode('<br />', $errors)); return false; } return true; } /** * Handles the post request method. * * @throws PrestaShopDatabaseException * @throws PrestaShopException * @author luigi * * @return bool|string|null */ public function postProcess() { if (Tools::isSubmit('submit')) { $name = (string)Tools::getValue('name'); if ($clientId = (int)Tools::getValue('id')) { $this->clientManager->updateProcess($name, $clientId, $_FILES['image']['tmp_name']); Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=4&configure=' . $this->name); } else { $this->clientManager->insertProcess($name); Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', true) . '&conf=3&configure=' . $this->name); } } return $this->output; } /** * Display form in the back office. * * @throws PrestaShopDatabaseException * @throws PrestaShopException * @author luigi * * @return string */ public function renderAddForm() { $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Impostazione '), ), 'input' => array( array( 'type' => 'categories', 'label' => $this->l('Seleziona Categoria'), 'name' => 'name', 'tree' => array( 'id' => 'category', 'selected_categories' => array((int)Configuration::get('category')), 'use_search' => true, 'use_checkbox' => false, 'name' => 'name', 'required' => true ) ), array( 'type' => 'file_lang', 'label' => $this->l('Seleziona Immagine'), 'name' => 'image', 'required' => true ), ), 'submit' => array( 'title' => $this->l('Save'), 'class' => 'btn btn-default pull-right' ) ); $helper = $this->helperFormConfiguration('submit'); $helper->show_cancel_button = true; $helper->fields_value['name'] = Configuration::get('name'); if (Tools::isSubmit('id')) { $getClient = $this->getClient((int)Tools::getValue('id')); $fields_form[0]['form']['input'][] = array('type' => 'hidden', 'name' => 'id'); $fields_form[0]['form']['image'] = $getClient[0]['image']; $has_picture = true; if ($has_picture) { $fields_form[0]['form']['input'][] = array('type' => 'hidden', 'name' => 'has_picture'); } $helper->tpl_vars = array( 'fields_value' => $this->getFormValues(), 'image_baseurl' => _PS_BASE_URL_ . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'c' . DIRECTORY_SEPARATOR, ); } return $helper->generateForm($fields_form); } /** * Render the form for displaying title and description in homepage. * * @author luigi * * @return string */ public function renderForm() { $fields_form[0]['form'] = array( 'legend' => array( 'title' => $this->l('Client Form'), ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Title'), 'name' => 'title', 'required' => true ), array( 'type' => 'text', 'label' => $this->l('Description'), 'name' => 'description', 'required' => true ), ), 'submit' => array( 'title' => $this->l('Save'), 'class' => 'btn btn-default pull-right' ) ); $helper = $this->helperFormConfiguration('submitText'); $helper->fields_value['title'] = Configuration::get('title'); $helper->fields_value['description'] = Configuration::get('description'); return $helper->generateForm($fields_form); } /** * The configuration for the form. * * @param string $nameAttribute | name of the button attribute. * * @author luigi * * @return HelperForm */ public function helperFormConfiguration(string $nameAttribute) { $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = $this->name; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->title = $this->displayName; $helper->show_toolbar = true; $helper->toolbar_scroll = true; $helper->submit_action = $nameAttribute; $helper->toolbar_btn = array( 'save' => array( 'desc' => $this->l('Save'), 'href' => AdminController::$currentIndex.'&configure='.$this->name.'&save'.$this->name. '&token='.Tools::getAdminTokenLite('AdminModules'), ) ); return $helper; } /** * Get the values of current client in the update section. * * @author luigi * @throws PrestaShopDatabaseException * * @return array */ public function getFormValues() { $fields = array(); $getClient = $this->getClient((int)Tools::getValue('id')); if (Tools::isSubmit('id')) { $fields['id'] = (int)Tools::getValue('id', $getClient[0]['id']); } $fields['has_picture'] = true; $fields['name'] = (string)Tools::getValue('name', $getClient[0]['name']); return $fields; } /** * Display list of client. * * @throws PrestaShopDatabaseException * @author luigi * * @return string */ public function renderList() { $this->getValues(); return $this->display(__FILE__, 'views/templates/admin/list.tpl'); } /** * Assigning values in the variable to be passed in the template. * * @throws PrestaShopDatabaseException * @author luigi * * @return $this */ public function getValues() { $this->context->smarty->assign( array( 'clients' => $this->getAll(), 'imgPath' => _PS_BASE_URL_ . DIRECTORY_SEPARATOR . 'img' . DIRECTORY_SEPARATOR . 'c' . DIRECTORY_SEPARATOR, 'link' => $this->context->link, 'moduleName' => $this->name, 'title' => Configuration::get('title'), 'description' => Configuration::get('description') ) ); return $this; } /** * Removing the images in the /images directory. * * @param string $imageName | filename of the target to be deleted. * * @author luigi * * @return bool */ public function deleteImage(string $imageName) { return $this->clientManager->deleteImage($imageName); } /** * Delete a client. * * @param int $id | id of the client. * * @throws PrestaShopDatabaseException * @throws PrestaShopException * @author luigi * * @return $this */ public function deleteClient(int $id) { $this->clientManager->deleteClient($id); Tools::redirectAdmin( $this->context->link->getAdminLink('AdminModules', true) . '&conf=1&configure=' . $this->name ); return $this; } /** * View client informations. * * @param int $id | id of the client. * * @throws PrestaShopDatabaseException * @author luigi * * @return $this */ public function showClient(int $id) { $this->context->smarty->assign( array( 'client' => $this->getClient($id) ) ); $this->output = $this->display(__FILE__, 'views/templates/admin/show.tpl'); return $this->output; } /** * Get all the clients in the database. * * @throws PrestaShopDatabaseException * @author luigi * * @return array|false|mysqli_result|PDOStatement|resource|null */ public function getAll() { return $this->clientManager->getAll(); } /** * Check if the client name is already exist in database. * * @param string $clientName | name of the client. * * @throws PrestaShopDatabaseException * @author luigi * * @return bool */ public function isClientExist(string $clientName) { return $this->clientManager->isClientExist($clientName); } /** * Return single client. * * @param int $clientId | id the the current client. * * @throws PrestaShopDatabaseException * @author luigi * * @return array|false|mysqli_result|PDOStatement|resource|null */ public function getClient(int $clientId) { return $this->clientManager->getClient($clientId); } /** * Insert clients in the database. * * @param string $name | the client name. * @param string $file_name | the image name. * * @throws PrestaShopDatabaseException * @author luigi * * @return bool */ public function insertSql(string $name, string $file_name) { return $this->clientManager->insertSql($name, $file_name); } /** * Update clients information. * * @param string $name | client name. * @param int $id | current if of the client * @param string|null $image | name of uploaded file. * * @author luigi * * @return bool */ public function updateSql(string $name, int $id, string $image = null) { return $this->clientManager->updateSql($name, $id, $image); } /** * Delete client. * * @param int $id | current id of the client. * * @author luigi * * @return bool */ public function deleteSql(int $id) { return $this->clientManager->deleteSql($id); } } questo è il codice Edited July 21, 2021 by AlexTed (see edit history) Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 Ciao, ci sono molti file mancanti come file modello e sql/createTable.php quindi non sono in grado di installare nel mio localhost e controllare. Puoi caricare il modulo completo. una volta scaricato puoi rimuovere il file zip Grazie Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 (edited) 8 minutes ago, SmartDataSoft said: Ciao, ci sono molti file mancanti come file modello e sql/createTable.php quindi non sono in grado di installare nel mio localhost e controllare. Puoi caricare il modulo completo. una volta scaricato puoi rimuovere il file zip Grazie ok allego il file dimmi quando hai scaricato che lo cancello Edited July 21, 2021 by AlexTed (see edit history) Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 Ho scaricato il modulo, puoi rimuoverlo grazie 1 Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 \modules\youmofulr\classes\ClientSqlQueries.php due funzioni che ho modificato public function getClient(int $clientId) { $sql = 'SELECT * FROM '._DB_PREFIX_.'clients as cl INNER JOIN `'._DB_PREFIX_.'customer` as cus ON cus.id_customer = cl.id WHERE cl.id = ' . pSQL($clientId) ; return Db::getInstance() ->executeS( $sql ); } public function getAll() { $sql = 'SELECT * FROM '._DB_PREFIX_.'clients as cl INNER JOIN `'._DB_PREFIX_.'customer` as cus ON cus.id_customer = cl.id '; return Db::getInstance() ->executeS( $sql ); } e yourmodule\views\templates\admin\show.tpl <p><strong>Name:</strong> {$client[0].firstname} </p> spero che ora tu sia in grado di risolvere i problemi grazie 1 Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 (edited) ok grazie ma come posso modificare id categoria con il nome della categoria ? durante l'aggiunta di una immagine, io seleziono e salvo id della categoria, ma vorrei che con una query mi restituisca il nome Edited July 21, 2021 by AlexTed (see edit history) Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 SELECT * FROM `rrs_clients` as cl INNER JOIN rrs_category_lang as cat_ln ON cl.name = cat_ln.id_category 1 Link to comment Share on other sites More sharing options...
AlexTed Posted July 21, 2021 Author Share Posted July 21, 2021 1 minute ago, SmartDataSoft said: SELECT * FROM `rrs_clients` as cl INNER JOIN rrs_category_lang as cat_ln ON cl.name = cat_ln.id_category grazie amico adesso è perfetto Link to comment Share on other sites More sharing options...
SmartDataSoft Posted July 21, 2021 Share Posted July 21, 2021 @AlexTed non dimenticare di contrassegnare il tuo post come soove e fare clic sul pulsante d'amore per mostrare il tuo amore 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now