简体   繁体   English

如何将变量添加到while循环中导致php

[英]How to add variables to while loop results in php

Hello I know I am new to php and coding but I have looked for days in forums and videos and i still have not found a clear answer. 您好,我知道我是php和编码的新手,但是我在论坛和视频中找了几天,但仍然没有找到明确的答案。 I have searched thoroughly before I attempted to bother you kind people. 在尝试打扰您之前,我已经进行了彻底的搜索。

I have 3 divs 我有3个div

<div id="left_div"></div>
<div id="middle_div"></div>
<div id="right_div"></div>

I have a while loop that outputs some color data into left_div 我有一个while循环,将一些颜色数据输出到left_div

$query = mysql_query("SELECT * FROM colorstyle");
while($data = mysql_fetch_array($query)){
echo "$data[1]";
 }

when I echo data my results are 当我回显数据时,我的结果是

blue 蓝色

red 红色

green 绿色

purple 紫色

and so on, and if the user do not see their color they can update the database's color table.... nothing new all this code works fine. 等等,如果用户看不到他们的颜色,他们可以更新数据库的颜色表。

Note* The color database is accessible to all users 注意*颜色数据库可供所有用户使用

In the middle_div I have a form with ADD and Delete Buttons middle_div我有一个带有ADD和Delete按钮的表单


The Process: 流程:

The user goes to the color_update_page.php 用户转到color_update_page.php

The left_div show the available colors from the global database. left_div显示全局数据库中可用的颜色。

The right_div shows the colors the user selected from the global database and added to their color table in the database right_div显示用户从全局数据库中选择并添加到数据库中颜色表中的颜色

When the user sees a color they want to add in the left_div , they click the color, then click the add button in the middle_div , and it adds it to the table in their color database in the right_div 当用户看到他们希望在添加颜色left_div ,他们单击颜色,然后单击添加按钮middle_div ,而且它在添加到表中根据其颜色数据库right_div


How I tried to accomplish this 我如何做到这一点

To make the while loop results clickable I used html and added a button to the output 为了使while循环结果可点击,我使用了html并在输出中添加了一个按钮

$query = mysql_query("SELECT * FROM colorstyle");
while($data = mysql_fetch_array($query)){
echo "<button value=\"$data[1]\" > </button><br>";
 }

To get the value of the button to a form I added an input field next to the button with the button value, this way i wouldn't have to worry about the user having to fill out a value into the input field...so now it look like this 为了将按钮的值添加到表单中,我在按钮旁边添加了带有按钮值的输入字段,这样,我就不必担心用户必须在输入字段中填写值...所以现在看起来像这样

$query = mysql_query("SELECT * FROM colorstyle");
while($data = mysql_fetch_array($query)){
echo "<input type='text' value=\"$data[1]\"><button value=\"$data[1]\" > </button><br>";
 }

next step I was going to have a hidden input to capture the value of the echoed input field..... so now the code looks like this 下一步,我将有一个隐藏的输入来捕获回显输入字段的值.....所以现在代码看起来像这样

$query = mysql_query("SELECT * FROM colorstyle");
while($data = mysql_fetch_array($query)){
echo "<input type='text' value=\"$data[1]\"><button value=\"$data[1]\" > </button>
<br>";
 }

   <input type="text" value="" style="display:none">......  lets call this this_will_change

I was then going to use javascript and an onclick function to change the value of the userInput .... So when the user clicks the echo generated button, the value from the echo generated input is transferred to and changed the field thats hidden... Then when the add button is clicked the users color table is then updated... 然后,我将要使用javascript和onclick函数来更改userInput的值...。因此,当用户单击生成回显的按钮时,来自生成的回显输入的值将传输到该字段并更改了隐藏的字段。然后,当单击添加按钮时,用户色表将被更新...

Heres the javascript and the corresponding php... 继承人的JavaScript和相应的PHP ...

Please Note: id='userInput' was added to the generated input tag....and id="this_will_change" was added to the hidden input field. 请注意: id='userInput'已添加到生成的输入标签中。...并且id="this_will_change"已添加到隐藏的输入字段中。

<script type=\"text/javascript\">
   function changeText2(){
   var userInput = document.getElementById('$data[1]').value;
   document.getElementById('this_will_change').innerHTML = $data[1];
   }
</script>

<?php
       $query = mysql_query("SELECT * FROM colorstyle");
while($data = mysql_fetch_array($query)){
echo "<input id='userInput' type='text' value=\"$data[1]\"><button value=\"$data[1]\" onclick='changeText2()'  > </button>
<br>";
 }

   <input type="text" value="" style="display:none" id="this_will_change">......  
?>

The Problems I faced: 我面临的问题:

I did not know I can not access data outside of a while loop array.... so I added the code inside the echoed while loop. 我不知道我无法在while循环数组之外访问数据....所以我在echo的while循环中添加了代码。 Not only was this messy when I looked at the view source code I could see that the javascript was repeated along with the while loop. 当我查看视图源代码时,这不仅很凌乱,而且可以看到JavaScript重复了while循环。

Also in order for it to work I would have to add the ADD and Delete forms to the while loop as well which looped and duplicate the form as well. 另外,为了使其正常工作,我还必须将ADD和Delete表单添加到while循环中,从而循环并复制该表单。

I tried to do a work around and put the while loop results into a variable with this 我试图解决这个问题,并将while循环结果放入此变量中

$query = mysql_query("SELECT * FROM colors");

while($data = mysql_fetch_array($query)){
echo "$data[0]";
$results[] = $data['0']
}
$results = implode("<br> ",$results);
echo "<button>$results</button>";

This woks but it echos the entire array into one large single button... not like before when all the results of the array were separated.. 这样就可以了,但是它会将整个数组呼应到一个大的单个按钮中……与之前分离数组的所有结果时不同。


What I am asking? 我在问什么?

How can I dynamically add a variable to each result from the while loop.. Since I have no idea what colors a user will want to add there is no way I can add a normal variable with a value unless I do it manually with the user adding colors then I would have to manually create the variable 我该如何动态地向while循环的每个结果中添加一个变量。由于我不知道用户想要添加哪种颜色,所以除非我与用户手动进行操作,否则我无法添加带有值的普通变量。添加颜色,那么我将不得不手动创建变量

Meaning if the results from the while loop are 这意味着while循环的结果是否为

Blue 蓝色

Red 红色

Orange and so on 橙色等

How can I change that to 我如何将其更改为

$blue = 'blue';

$red = 'red';

$orange = 'orange';

I know there has to be a way to get this done as i see similar versions on other websites. 我知道必须有一种方法来完成此操作,因为我在其他网站上看到了类似的版本。 Like on iOffer.com. 就像在iOffer.com上一样。 If there is a country you do not want to sell to you can click that country in the left_div (the country is then highlighted)...then click add in the middle_div ..........and your list of restricted countries will be updated. 如果有一个国家不希望出售给您,则可以在left_div单击该国家(然后突出显示该国家)...然后在middle_div添加..........以及您的清单受限制的国家/地区将会更新。


What I have looked into: 我调查的内容:

I've looked into variable variables but every keep saying those shouldn't be done and I didn't quite understand it. 我已经研究了变量变量,但是每个人都说不应该这样做,而且我不太理解。

I've read many ways on using AJAX but I am not there yet. 我已经阅读了许多使用AJAX的方法,但我还没有。

And yes, I know MySQLi, OOP, and jQuery are better for coding but I want to get a grasp on using mysql and javascript before i get to that. 是的,我知道MySQLi,OOP和jQuery更适合编码,但我想在使用mysql和javascript之前先掌握一下。 I want to code this website using mysql then upgrade the site using those techniques.. Please try to limit the responses to javascript if needed and mysql. 我想使用mysql对这个网站进行编码,然后使用这些技术升级网站。.如果需要,请尝试限制对javascript和mysql的响应。

Apologies: 道歉:

I am sorry for being long winded with my ignorance on php. 很抱歉,我对php的无知已久。 But I have read so many times that the poster never gave a clear description of the problem they are having and what they are trying to accomplish. 但是我读了很多遍,以至于海报从未清楚地描述他们所遇到的问题以及他们要解决的问题。 Hopefully I was clear and not irritating. 希望我很清楚并且没有烦恼。 Thank you in advance for your help. 预先感谢您的帮助。

Here is how it looks: 外观如下:

<head>
  <style>

 div{
 text-align:center;
 }

 li{
 list-style:none;
 }

 #color_select{
 overflow:auto;
 width:706px;

 #color_select li{
 float:left;

 #left_div{
 border:1px solid black;
 width:300px;

 #middle_div{
 border:1px solid black;
 width:100px;

 #right_div{
 border:1px solid black;
 width:300px;
}
 </style>
</head>

        <body>
<nav id="color_select">
    <ul>
       <li>
          <div id="left_div">
          <label>Available Colors</label>
          </div>
      </li>
      <li>
          <div id="middle_div">
          <label>Available Colors</label>
          </div>
      </li>
      <li>
          <div id="right_div">
          <label>Available Colors</label>
          </div>
      </li>
    </ul>
</nav>
</body>


</html>

Without using javascript at all , this is how I would consider tackling this: 完全不使用javascript ,这就是我考虑的解决方法:

  • Fetch your colours from the database into a single variable $colours 从数据库中获取颜色到单个变量$colours
  • Get the colours that are selected already by accessing the POST vars ( $coloursSelected = $_POST['colours'] ) 通过访问POST变量获取已经选择的颜色( $coloursSelected = $_POST['colours']
  • Begin your form ( <form method="post"> ) 开始您的表单( <form method="post">
  • Create a div that is hidden ( style="display: none;" ) 创建一个隐藏的div( style="display: none;"
  • Loop through your colours and add them as checkbox elements in the hidden div ( <input type="checkbox" name="colours[]" value="{$colour}" /> ) - You will need to make sure that the selected="selected" attribute appears if it has been selected (in $coloursSelected ) 循环遍历颜色并将其作为复选框元素添加到隐藏的div中( <input type="checkbox" name="colours[]" value="{$colour}" /> )-您需要确保selected="selected"如果已选择selected="selected"属性(以$coloursSelected
  • Do your HTML for your three columns 为您的三栏做HTML
  • In the first column you will loop the difference of all your colours and those that are selected ( $unselectedColours = array_diff($colours, $coloursSelected); ) 在第一列中,将循环显示所有颜色和所选颜色之间的差异( $unselectedColours = array_diff($colours, $coloursSelected);
  • Echo those colours to the page as buttons like the following: 将这些颜色作为按钮返回到页面,如下所示:

    <input type="submit" name="added" value="{$color}" />

  • In the right hand column just loop the colours that are already selected and echo buttons in a similar way: 在右侧列中,以类似的方式循环选择已经选择的颜色并按回显按钮:

    <input type="submit" name="removed" value="{$color}" />

This is missing a few key steps because this isn't a "write it for you" website. 这缺少一些关键步骤,因为这不是“为您编写”网站。 You should add a var_dump($_POST) to your code so you can understand what's going on. 您应该在var_dump($_POST)添加一个var_dump($_POST) ,以便您了解发生了什么。

I hope that pushes you in the right direction. 我希望这能将您推向正确的方向。

Whenever you see you're repeating something, it usually means you're doing something wrong. 每当您看到重复的内容时,通常意味着您做错了什么。

You approach is wrong here - no need to query the database so many times. 您的方法在这里是错误的-无需多次查询数据库。 You fetch the data once, put it into an array and then use that array all the time. 您只提取一次数据,将其放入一个数组中,然后一直使用该数组。

I see you've tried something like that here: 我看到您在这里尝试过类似的操作:

$query = mysql_query("SELECT * FROM colors");

while($data = mysql_fetch_array($query)){
echo "$data[0]";
$results[] = $data['0']
}
$results = implode("<br> ",$results);
echo "<button>$results</button>";

One echo will print one thing - and here it's one button with an imploded array. 一个回声将打印一件事-这是一个带有内爆数组的按钮。 Not good. 不好。 You want to loop over all the results and create a button for each. 您想遍历所有结果并为每个结果创建一个按钮。 So: 所以:

$query = mysql_query("SELECT * FROM colors");

while($data = mysql_fetch_array($query)){
    echo "$data[0]";
    $results[] = $data['0']
}
for($i = 0; $i < count($results); $i++) {
    echo '<button>', $results[$i], '</button><br/>';
}

Now this will output something like: 现在,这将输出如下内容:

<button>Blue</button><br/>
<button>Red</button><br/>
<button>Orange</button><br/>

A good idea here is to add some JavaScript controls over those buttons. 这里的一个好主意是在这些按钮上添加一些JavaScript控件。 Let's add an onclick handler that will call some function and pass the color index as a parameter. 让我们添加一个onclick处理程序,该处理程序将调用某些函数并将颜色索引作为参数传递。 The modified loop now looks like this: 修改后的循环现在看起来像这样:

for($i = 0; $i < count($results); $i++) {
    echo "<button onclick='buttonClicked($i)'>", $results[$i], '</button><br/>';
}

And btw, jQuery is not better for coding than plain JavaScript - easier to learn, yes... but also slower and heavier (the size of the library), and in the end, it still uses plain JavaScript. 顺便说一句,jQuery在编码方面并不比普通的JavaScript更好-易于学习,是的……但是速度也越来越慢(库的大小),最后,它仍然使用普通的JavaScript。 You'll definitely be a better developer if you have a solid knowledge of JavaScript rather than just jQuery. 如果您对JavaScript有扎实的知识,而不仅仅是jQuery,那么您一定会是一个更好的开发人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM