Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [MySQL][PHP][AJAX] Rozwijana lista, Potrzebna pomoc przy przerobieniu skryptu.
--Nowicjusz--
post 21.02.2012, 10:14:41
Post #1





Goście







Witam,

Próbuję przerobić ten skrypt: http://www.coursesweb.net/ajax/multiple-se...own-list-ajax_t

1) Jak zrobić żeby zamiast Select: [nazwa_kolumny] [lista] było Select: Name (czyli mój tekst) [lista] ?
2) Wynik wywołania selecta próbuję przedstawić w tabeli ale nie do końca mi to wychodzi. Potrzebuję żeby każda pozycja była w osobnym <td>.
3) Jak zrobić aby obok wyniku selecta pojawiała się wartość z innej kolumny np. stock_fr ?

Czyli ma być tak:

  1. LP. | WYNIK SELECTA | STOCK_FR


  1. <?php
  2. if(!isset($_SESSION)) session_start();
  3.  
  4. // Ustawienie bazy danych
  5. $server = 'localhost';
  6. $user = 'admin;
  7. $pass = '1234';
  8. $dbase = 'admin';
  9.  
  10. $table = 'magazyn'; // nazwa tabeli
  11. $ar_cols = array('name_fr', 'color_fr', 'size_fr'); // kolumny z których czerpie dane
  12.  
  13. $preid = 'slo_'; // a prefix used for element's ID, in which Ajax will add <select>
  14. $col = $ar_cols[0]; // the variable used for the column that wil be selected
  15. $re_html = ''; // will store the returned html code
  16.  
  17. // if there is data sent via POST, with index 'col' and 'wval'
  18. if(isset($_POST['col']) && isset($_POST['wval'])) {
  19. // set the $col that will be selected and the value for WHERE (delete tags and external spaces in $_POST)
  20. $col = trim(strip_tags($_POST['col']));
  21. $wval = "'".trim(strip_tags($_POST['wval']))."'";
  22. }
  23.  
  24. $key = array_search($col, $ar_cols); // get the key associated with the value of $col in $ar_cols
  25. $wcol = $key===0 ? $col : $ar_cols[$key-1]; // gets the column for the WHERE clause
  26. $_SESSION['ar_cols'][$wcol] = isset($wval) ? $wval : $wcol; // store in SESSION the column and its value for WHERE
  27.  
  28. // gets the next element in $ar_cols (needed in the onchange() function in <select> tag)
  29. $last_key = count($ar_cols)-1;
  30. $next_col = $key<$last_key ? $ar_cols[$key+1] : '';
  31.  
  32. $conn = new mysqli($server, $user, $pass, $dbase); // connect to the MySQL database
  33.  
  34. if (mysqli_connect_errno()) { exit('Connect failed: '. mysqli_connect_error()); } // check connection
  35.  
  36. // sets an array with data of the WHERE condition (column=value) for SELECT query
  37. for($i=1; $i<=$key; $i++) {
  38. $ar_where[] = '`'.$ar_cols[$i-1].'`='.$_SESSION['ar_cols'][$ar_cols[$i-1]];
  39. }
  40.  
  41. // define a string with the WHERE condition, and then the SELECT query
  42. $where = isset($ar_where) ? ' WHERE '. implode($ar_where, ' AND ') : '';
  43. $sql = "SELECT DISTINCT `$col` FROM `$table`".$where;
  44.  
  45. $result = $conn->query($sql); // perform the query and store the result
  46.  
  47. // if the $result contains at least one row
  48. if ($result->num_rows > 0) {
  49. // sets the "onchange" event, which is added in <select> tag
  50. $onchg = $next_col!==null ? " onchange=\"ajaxReq('$next_col', this.value);\"" : '';
  51.  
  52. // sets the select tag list (and the first <option>), if it's not the last column
  53. if($col!=$ar_cols[$last_key]) $re_html = $col. ': <select name="'. $col. '"'. $onchg. '><option>- - -</option>';
  54.  
  55. while($row = $result->fetch_assoc()) {
  56. // if its the last column, reurns its data, else, adds data in OPTION tags
  57. if($col==$ar_cols[$last_key]) $re_html .= '<br/>'. $row[$col];
  58. else $re_html .= '<option value="'. $row[$col]. '">'. $row[$col]. '</option>';
  59. }
  60.  
  61. if($col!=$ar_cols[$last_key]) $re_html .= '</select> '; // ends the Select list
  62. }
  63. else { $re_html = '0 results'; }
  64.  
  65. $conn->close();
  66. $tekst = '1111';
  67. // if the selected column, $col, is the first column in $ar_cols
  68. if($col==$ar_cols[0]) {
  69. // adds html code with SPAN (or DIV for last item) where Ajax will add the select dropdown lists
  70. // with ID in each SPAN, according to the columns added in $ar_cols
  71. for($i=1; $i<count($ar_cols); $i++) {
  72. if($ar_cols[$i]===null) continue;
  73. if($i==$last_key) $re_html .= '
  74. <table bgcolor="#f4f4f4" width=100% style="font-size: 11px; font-family: Tahoma" border="1" cellpadding="10" cellspacing="1">
  75. <tr bgcolor="#ff9320" style="font-color: #ffffff" ><th>Rozmiary</th></tr>
  76.  
  77.  
  78. <tr><td id="'. $preid.$ar_cols[$i].'"></td></tr>
  79. </table>';
  80. else $re_html .= '<font color="red"><span id="'. $preid.$ar_cols[$i].'"></span></font>';
  81. }
  82.  
  83. // adds the columns in JS (used in removeLists() to remove the next displayed lists when makes other selects)
  84. $re_html .= '<script type="text/javascript">var ar_cols = '.json_encode($ar_cols).'; var preid = "'. $preid .'";</script>';
  85. }
  86. else echo $re_html;
  87. ?>


Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.04.2024 - 15:43