Colin Posted July 7, 2004 Posted July 7, 2004 hey guys, I'm trying to build a page that will allow me to submit into my vehicles database a vehicle make and model. Here's the fun part. The code I pasted below works with one exception. What I can't figure out is how to have the script add a vehicle_model_id into the vehicles table that is checked against a vehicle_make table. Basically, what I need the script to do is check the vehicle_make table and compare all of the rows there (the vehicle manufacturers that have their own ID number) with the submitted information from this page. The script needs to submit into the vehicles table the corresponding vehicle_make id number. Why, you ask? I have a script written that checks the vehicle_make table entries as you go through a set of forms. The final results are built from the vehicles table. The code is reliant on the vehicle_make_id being passed. The script all works, I just can't figure out how to check the vehicle_make from the text box on the page below with the vehicle_make_id in the vehicle_make table and insert the corresponding vehicle_make_id into the vehicles table. Any ideas? Thanks! Colin <?php /* $Id: vehicles.php,v 1.49 2003/06/29 22:50:51 hpdl Exp $ osCommerce, Open Source E-Commerce Solutions http://www.oscommerce.com Copyright (c) 2003 osCommerce Portions of currencies.php code modifed by Colin Opseth (c) 2004 Webmaster: www.GMPerformance.org Webmaster: www.AutoGoodies.com Email: [email protected] Please give credit if you use portions of this script. Thanks! Released under the GNU General Public License */ require('includes/application_top.php'); $vehicles = new vehicles(); //// // Class to handle vehicles // TABLES: vehicles class vehicles { var $vehicles; // class constructor function vehicles() { $this->vehicles = array(); $vehicles_query = tep_db_query("select ve.vehicle_model, ve.vehicle_make, ve.value from " . TABLE_VEHICLES . " ve left join " . TABLE_MAKE . " ma on (ve.vehicle_make = ma.vehicle_make) where ve.vehicle_make_id = ma.vehicle_make_id"); while ($vehicles = tep_db_fetch_array($vehicles_query)) { $this->vehicles[$vehicles['vehicle_model']] = array('vehicle_make' => $vehicles['vehicle_make'], 'vehicle_make_id' => $vehicles['vehicle_make_id'], 'value' => $vehicles['value']); } } function get_value($vehicle_model) { return $this->vehicles[$vehicle_model]['value']; } } $action = (isset($HTTP_GET_VARS['action']) ? $HTTP_GET_VARS['action'] : ''); if (tep_not_null($action)) { switch ($action) { case 'insert': case 'save': if (isset($HTTP_GET_VARS['cID'])) $vehicle_id = tep_db_prepare_input($HTTP_GET_VARS['cID']); $vehicle_make = tep_db_prepare_input($HTTP_POST_VARS['vehicle_make']); $make = tep_db_prepare_input($HTTP_POST_VARS['vehicle_make']); $vehicle_model = tep_db_prepare_input($HTTP_POST_VARS['vehicle_model']); $value = tep_db_prepare_input($HTTP_POST_VARS['value']); $sql_data_array = array('vehicle_make' => $vehicle_make, 'vehicle_make' => $make, 'vehicle_model' => $vehicle_model, 'value' => $value); if ($action == 'insert') { tep_db_perform(TABLE_VEHICLES, $sql_data_array); $vehicle_id = tep_db_insert_id(); } elseif ($action == 'save') { tep_db_perform(TABLE_VEHICLES, $sql_data_array, 'update', "vehicle_model_id = '" . (int)$vehicle_id . "'"); } tep_redirect(tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $vehicle_id)); break; case 'deleteconfirm': $vehicle_model_id = tep_db_prepare_input($HTTP_GET_VARS['cID']); $vehicle_query = tep_db_query("select vehicle_model_id from " . TABLE_VEHICLES . " where vehicle_model = '" . DEFAULT_CURRENCY . "'"); $vehicle = tep_db_fetch_array($vehicle_query); if ($vehicle['vehicle_model_id'] == $vehicle_model_id) { tep_db_query("update " . TABLE_CONFIGURATION . " set configuration_value = '' where configuration_key = 'DEFAULT_CURRENCY'"); } tep_db_query("delete from " . TABLE_VEHICLES . " where vehicle_model_id = '" . (int)$vehicle_model_id . "'"); tep_redirect(tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'])); break; case 'update': $server_used = CURRENCY_SERVER_PRIMARY; $vehicle_query = tep_db_query("select ve.vehicle_model_id, ve.vehicle_model, ve.vehicle_make, ve.vehicle_make_id from " . TABLE_VEHICLES . " ve left join " . TABLE_MAKE . " ma on (ve.vehicle_make = ma.vehicle_make)"); while ($vehicle = tep_db_fetch_array($vehicle_query)) { $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_vehicle'; $rate = $quote_function($vehicle['vehicle_model']); if (empty($rate) && (tep_not_null(CURRENCY_SERVER_BACKUP))) { $messageStack->add_session(sprintf(WARNING_PRIMARY_SERVER_FAILED, CURRENCY_SERVER_PRIMARY, $vehicle['vehicle_make'], $vehicle['vehicle_model']), 'warning'); $quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_vehicle'; $rate = $quote_function($vehicle['vehicle_model']); $server_used = CURRENCY_SERVER_BACKUP; } if (tep_not_null($rate)) { tep_db_query("update " . TABLE_VEHICLES . " set value = '" . $rate . "', last_updated = now() where vehicle_model_id = '" . (int)$vehicle['vehicle_model_id'] . "'"); $messageStack->add_session(sprintf(TEXT_INFO_CURRENCY_UPDATED, $vehicle['vehicle_make'], $vehicle['vehicle_model'], $server_used), 'success'); } else { $messageStack->add_session(sprintf(ERROR_CURRENCY_INVALID, $vehicle['vehicle_make'], $vehicle['vehicle_model'], $server_used), 'error'); } } tep_redirect(tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $HTTP_GET_VARS['cID'])); break; case 'delete': $vehicle_model_id = tep_db_prepare_input($HTTP_GET_VARS['cID']); $vehicle_query = tep_db_query("select vehicle_model from " . TABLE_VEHICLES . " where vehicle_model_id = '" . (int)$vehicle_model_id . "'"); $vehicle = tep_db_fetch_array($vehicle_query); $remove_vehicle = true; if ($vehicle['vehicle_model'] == DEFAULT_CURRENCY) { $remove_vehicle = false; $messageStack->add(ERROR_REMOVE_DEFAULT_CURRENCY, 'error'); } break; } } ?> <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"> <html <?php echo HTML_PARAMS; ?>> <head> <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> <title><?php echo TITLE; ?></title> <link rel="stylesheet" type="text/css" href="includes/stylesheet.css"> <script language="javascript" src="includes/general.js"></script> </head> <body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF" onload="SetFocus();"> <!-- header //--> <?php require(DIR_WS_INCLUDES . 'header.php'); ?> <!-- header_eof //--> <!-- body //--> <table border="0" width="100%" cellspacing="2" cellpadding="2"> <tr> <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft"> <!-- left_navigation //--> <?php require(DIR_WS_INCLUDES . 'column_left.php'); ?> <!-- left_navigation_eof //--> </table></td> <!-- body_text //--> <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr> <td><table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td class="pageHeading"><?php echo HEADING_TITLE; ?></td> <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td> </tr> </table></td> </tr> <tr> <td><table border="0" width="100%" cellspacing="0" cellpadding="0"> <tr> <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr class="dataTableHeadingRow"> <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_MAKE_NAME; ?></td> <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_MODEL_CODES; ?></td> <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_CURRENCY_VALUE; ?></td> <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?> </td> </tr> <?php $vehicle_query_raw = "select vehicle_model_id, vehicle_make, vehicle_model, last_updated, value from " . TABLE_VEHICLES . " order by vehicle_make"; $vehicle_split = new splitPageResults($HTTP_GET_VARS['page'], MAX_DISPLAY_SEARCH_RESULTS, $vehicle_query_raw, $vehicle_query_numrows); $vehicle_query = tep_db_query($vehicle_query_raw); while ($vehicle = tep_db_fetch_array($vehicle_query)) { if ((!isset($HTTP_GET_VARS['cID']) || (isset($HTTP_GET_VARS['cID']) && ($HTTP_GET_VARS['cID'] == $vehicle['vehicle_model_id']))) && !isset($cInfo) && (substr($action, 0, 3) != 'new')) { $cInfo = new objectInfo($vehicle); } if (isset($cInfo) && is_object($cInfo) && ($vehicle['vehicle_model_id'] == $cInfo->vehicle_model_id) ) { echo ' <tr id="defaultSelected" class="dataTableRowSelected" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=edit') . '\'">' . "\n"; } else { echo ' <tr class="dataTableRow" onmouseover="rowOverEffect(this)" onmouseout="rowOutEffect(this)" onclick="document.location.href=\'' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $vehicle['vehicle_model_id']) . '\'">' . "\n"; } if (DEFAULT_CURRENCY == $vehicle['vehicle_model']) { echo ' <td class="dataTableContent"><b>' . $vehicle['vehicle_make'] . ' (' . TEXT_DEFAULT . ')</b></td>' . "\n"; } else { echo ' <td class="dataTableContent">' . $vehicle['vehicle_make'] . '</td>' . "\n"; } ?> <td class="dataTableContent"><?php echo $vehicle['vehicle_model']; ?></td> <td class="dataTableContent" align="right"><?php echo number_format($vehicle['value'], 8); ?></td> <td class="dataTableContent" align="right"><?php if (isset($cInfo) && is_object($cInfo) && ($vehicle['vehicle_model_id'] == $cInfo->vehicle_model_id) ) { echo tep_image(DIR_WS_IMAGES . 'icon_arrow_right.gif'); } else { echo '<a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $vehicle['vehicle_model_id']) . '">' . tep_image(DIR_WS_IMAGES . 'icon_info.gif', IMAGE_ICON_INFO) . '</a>'; } ?> </td> </tr> <?php } ?> <tr> <td colspan="4"><table border="0" width="100%" cellspacing="0" cellpadding="2"> <tr> <td class="smallText" valign="top"><?php echo $vehicle_split->display_count($vehicle_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, $HTTP_GET_VARS['page'], TEXT_DISPLAY_NUMBER_OF_VEHICLES); ?></td> <td class="smallText" align="right"><?php echo $vehicle_split->display_links($vehicle_query_numrows, MAX_DISPLAY_SEARCH_RESULTS, MAX_DISPLAY_PAGE_LINKS, $HTTP_GET_VARS['page']); ?></td> </tr> <?php if (empty($action)) { ?> <tr> <td colspan="2" align="right"><?php echo '<a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=new') . '">' . tep_image_button('button_new_vehicle.gif', IMAGE_NEW_VEHICLE) . '</a>'; ?></td> </tr> <?php } ?> </table></td> </tr> </table></td> <?php $heading = array(); $contents = array(); switch ($action) { case 'new': $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_NEW_CURRENCY . '</b>'); $contents = array('form' => tep_draw_form('vehicles', FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . (isset($cInfo) ? '&cID=' . $cInfo->vehicle_model_id : '') . '&action=insert')); $contents[] = array('text' => TEXT_INFO_INSERT_INTRO); $contents[] = array('text' => '<br>' . TEXT_INFO_MAKE_TITLE . '<br>' . tep_draw_input_field('vehicle_make')); $contents[] = array('text' => '<br>' . TEXT_INFO_MODEL_CODE . '<br>' . tep_draw_input_field('vehicle_model')); $contents[] = array('text' => '<br>' . TEXT_INFO_CURRENCY_VALUE . '<br>' . tep_draw_input_field('value')); $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_insert.gif', IMAGE_INSERT) . ' <a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $HTTP_GET_VARS['cID']) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'); break; case 'edit': $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_EDIT_CURRENCY . '</b>'); $contents = array('form' => tep_draw_form('vehicles', FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=save')); $contents[] = array('text' => TEXT_INFO_EDIT_INTRO); $contents[] = array('text' => '<br>' . TEXT_INFO_MAKE_TITLE . '<br>' . tep_draw_input_field('vehicle_make', $cInfo->vehicle_make)); $contents[] = array('text' => '<br>' . TEXT_INFO_MODEL_CODE . '<br>' . tep_draw_input_field('vehicle_model', $cInfo->vehicle_model)); $contents[] = array('text' => '<br>' . TEXT_INFO_CURRENCY_VALUE . '<br>' . tep_draw_input_field('value', $cInfo->value)); $contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_update.gif', IMAGE_UPDATE) . ' <a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'); break; case 'delete': $heading[] = array('text' => '<b>' . TEXT_INFO_HEADING_DELETE_VEHICLE . '</b>'); $contents[] = array('text' => TEXT_INFO_DELETE_INTRO); $contents[] = array('text' => '<br><b>' . $cInfo->vehicle_make . ' ' . $cInfo->vehicle_model . '</b>'); $contents[] = array('align' => 'center', 'text' => '<br>' . (($remove_vehicle) ? '<a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=deleteconfirm') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a>' : '') . ' <a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id) . '">' . tep_image_button('button_cancel.gif', IMAGE_CANCEL) . '</a>'); break; default: if (is_object($cInfo)) { $heading[] = array('text' => '<b>' . $cInfo->vehicle_make . '</b>'); $contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=edit') . '">' . tep_image_button('button_edit.gif', IMAGE_EDIT) . '</a> <a href="' . tep_href_link(FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . '&cID=' . $cInfo->vehicle_model_id . '&action=delete') . '">' . tep_image_button('button_delete.gif', IMAGE_DELETE) . '</a>'); $contents[] = array('text' => '<br>' . TEXT_INFO_MAKE_TITLE . ' ' . $cInfo->vehicle_make); $contents[] = array('text' => TEXT_INFO_MODEL_CODE . ' ' . $cInfo->vehicle_model); $contents[] = array('text' => '<br>' . TEXT_INFO_CURRENCY_LAST_UPDATED . ' ' . tep_date_short($cInfo->last_updated)); } break; } if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) { echo ' <td width="25%" valign="top">' . "\n"; $box = new box; echo $box->infoBox($heading, $contents); echo ' </td>' . "\n"; } ?> </tr> </table></td> </tr> </table></td> <!-- body_text_eof //--> </tr> </table> <!-- body_eof //--> <!-- footer //--> <?php require(DIR_WS_INCLUDES . 'footer.php'); ?> <!-- footer_eof //--> <br> </body> </html> <?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
Jaguar Posted July 15, 2004 Posted July 15, 2004 good question. Lets give it another shot on the board. Loren The great thing about beating your head against the wall is that it feels SO GOOD when you stop!
Colin Posted July 15, 2004 Author Posted July 15, 2004 thanks for the help.. I haven't yet figured it out, lol
Colin Posted July 15, 2004 Author Posted July 15, 2004 i did try adding a new line to the array but then the form wouldn't submit anything at all.. it went something like.. $contents = array('form' => tep_draw_hidden_form('vehicles', FILENAME_VEHICLES, 'page=' . $HTTP_GET_VARS['page'] . (isset($cInfo) ? '&cID=' . $cInfo->vehicle_model_id : '') . '&action=insert'));
Mary B. Posted July 17, 2004 Posted July 17, 2004 Ok, I'll take a shot at it. When you start processing the form results, you have the vehicle_make that was input to the form. To get the corresponding vehicle_make_id from the vehicle_make table, you need to run a preliminary query, selecting vehicle_make_id from vehicle_make where vehicle_make = $vehicle_make. Then you can run your insert or update query to put the vehicle_make_id into the vehicles table. They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety. ~ Benjamin Franklin, 1759. տլ
Recommended Posts
Archived
This topic is now archived and is closed to further replies.