I am fairly new to PHP and Yii, and the problem that I am not nor as the question in google, so the only thing I can think of is to ask the question to this list that I have solved many problems.
The issue is as follows: in the code that I attached, I read several records that I keep in array and after the process.
Well, if you look at the debug entries in foreach in he first, all goes well and the variable $items is loaded, but when I get to the second debug $items variable has the correct number of elements, but the elements are empty : count ($items) = 2 but $items[0] and $items[1] are null
$idiomas=CListaMidiomas::model()->findAll();
$items=array();
$nombre=array();
$a=0;
foreach ($idiomas as $idioma){
$nombre[$a]=$idioma->sIdioma;
$items[$a]=TblCategoriastexto::model()->findAll(
array('condition'=>'id='.$data->id.' AND idIdioma='.$idioma->id_idioma));
echo "<br>---AAAAAAAAAAA--".$a."-----------<br>";
CVarDumper::dump($items); //in this moment is correct
if (empty($items[$a]) ||$items[$a]==null ){ // not enter because $items have content
$items[$a]=new TblCategoriastexto();
$items[$a]->idIdioma=$idioma->id_idioma;
}
$a++;
}
echo ">>>>>>>>>>>>>>>".count($items) ; //<<<<<<<<<<present 2
CVarDumper::dump($items); // but in this moment t0 2 are null
for ($a=0;$a<count($items) ;$a++){
echo "<b>".CHtml::encode($nombre[$a]).":</b>";
$out="";
$item=$items[$a];
echo "<br>-----".$a."-----------<br>";
CVarDumper::dump($items[$a]);<<<<<<<<<<<<<<<<<<<<<<<<null
for ($b=1;$b<=20;$b++){
$campo="tc".$b;
$out.=$items[$a]->$campo . ",";<<<<<<<<<<<<<<<<error
}
echo CHtml::encode($out);
echo"<br>";
}
This line: if (empty($items[$a]) ||$items[$a]=null ){ will always assign $items[$a] to null.
To compare values, use the comparison (for equality) operator, == instead of the assignment operator =.
Try changing this line:
if(isset($items[$a]->$campo)) {
$out.=$items[$a]->$campo . ",";
}
Related
I'm currently working on a website for my project, but for some reason when everything is as it has to be ( session is started, session variable is defined ), then only array_push() function doesn't add the value to session variable, when I move array_push() out of if, foreach and if (if contains foreach and foreach contains another if), it works fine, I already tried to put it to first if, then to foreach but it didn't help. So it works only when it is out of that code (When it is not in that if, which contains foreach, which contains another if). Please try to understand, my English is not well.
What do I have to do to fix it?
if(isset($_GET["setLang"])) {
foreach ($languages as $item) {
if($item === $_GET["setLang"]) {
$_SESSION["selLang"] = $item;
array_push($_SESSION["sessionMessage"], "Language changed successfully!");
header("location: index.php?path=/");
exit();
}
}
}
Since the code work correctly out of the if statement , it's cleary evident that the condition cause the problem
$item === $_GET["setLang"]
Using triple = will check value and type . So may be the type of each ones is different than other .
Replace it with :
$item == $_GET["setLang"] // only use two =
I'm quite confused in how would I be able to create variable based on an array's values, my code is:
$a = array("red","black","white","green","blue");
for($i=0;$i>5;$i++)
{
$$a[$i] = '0.00';
}
echo $red;
I was under the impression that emulating a statement that says $red = '0.00'; would run properly but alas, it says undefined variable red.
use this:
for($i=0;$i<5;$i++)
you got error in loop, you have used '>' sign, so loop doesn't work, actually... :)
It's only your assignation that is wrong.
Use a foreach loop to make it easier, and it will work :
$a = array("red","black","white","green","blue");
foreach ($a as $val) {
$$val = '0.00';
}
echo $red;
Output :
0.00
I switched my code to PDO and I almost there, except for this part where all results from a query are supposed to be displayed with a foreach statement. I know the data is being fetched properly and is correctly stored in $row2.
What I need to do is get take the 'position' variable from $results2 and add 1 to it and then run the foreach loops where position is equal to that new number...
BEFORE I SWITCHED TO PDO, my code was working perfectly and this did correctly display the 2 expected results:
$cont = $results2[0]->position;;
$cont++;
foreach ($results2 as $resulting) {
if ( $results2[0]->position = $cont )
{
echo "<hr><br><br>" . $resulting->text . " " . " <b>Suggested by: $resulting-
>display_name </b><br>|<b> Approve this contribution</b> | <b>delete this suggestion</b> |";
}
}
But now, the foreach only returns the same first result twice after the conversion to PDO, which looks like this:
//NEW PDO CODE--DOESN'T DISPLAY BOTH RESULTS:
$results2->execute();
$row2 = $results2->fetchAll(PDO::FETCH_ASSOC);
$cont = $row2[0]['position'];
$cont++;
foreach ($row2 as $resulting) {
if ( $row2[0]['position'] == $cont )
{
echo "<hr><br><br>" . $row2[0][text] . " " . " <b>Suggested by:" . $row2[0]
[display_name] . "</b><br>|<b> Approve this contribution</b> | <b>
delete this suggestion</b> |";
}
}
It looks like your old code was fetching an array of objects, whereas your new code is fetching an array of arrays (indicated by PDO::FETCH_ASSOC).
So while $results2[0]->position used to work, you mistakenly replaced it with $row2[0][position]. However, position needs to be quoted in the second case since it is an array index.
If you had all of your error reporting turned on, you would have easily seen this.
The same applies to $row2[0][text] and $row2[0][display_name]. In addition to the lack of quotes on these indexes, you probably really want $resulting instead of $row[0] since $row[0] will give you the same values each time through the loop. So those should probably be changed to $resulting['text'] and $resulting['display_name']
So replace all instances of $row2[0][position] with $row2[0]['position']. Also, heed the advice of John Ruddell.
NOTE: this is not an answer... its just a way for me to clarify what I was saying in comments because its hard to show code in comments.
GENERAL IF CODE LOGIC:
if (true){
// code will always execute on true
}
if (false){
// code will never execute on false
}
sample:
$key = "12345";
if($key){
// will always be executed because this is a true statement.
}
your code:
if ( $row2[0]['position'] = $cont ){
// will always execute because the assignment is true
}
NOW:
with all that aside.. if nothing is happening when you run it with == that means that the count is not equal to the $row[0][position] so its a false statement and the code inside the if does not get executed.
RECOMMENDATION:
try putting an echo $cont; and echo $row2[0]['position']; before the if statement to see whats going on.
I was unable to break out of a foreach loop. I think the structure is correct there is something else wrong with the code. Please let me know what is the issue,(not just a working code) i want to learn from my mistakes. Thanks
I am using simple html dom for scraping some piece of information and i want the loop to break when a condition is matched. Here is my code :
<?php
$mainjob = file_get_html('link to scrap here');
$newarr = array();
foreach($mainjob->find('td[valign=middle]') as $d) {
$data = $d->innertext;
$newarr[] = $data;
echo $data . "<br>";
if($data == "Job Opportunity Description:") {
break;
}
}
print_r($newarr);
1) I guess your $data=="Job Opportunity Description:" condition wasn't true, so it didn't call the break;
2) Put your $mainjob->find('td[valign=middle]') out of your loop conditions, so it doesn't get called each iteration:
$tds = $mainjob->find('td[valign=middle]');
foreach($td as $d){}
3) Why did you add brackets to your $newarr variable?
4) Maybe your $data string still contains the html tags (which aren't shown by echo due to your browser.. so the condition would return false.
The problem is in the white space i used trim() to trim out the white space and it works. I recommend the answer from Ayman Safadi. Thanks.
I'm working with 3 different arrays (although I'm only testing with two for the time being) and I'm trying to process the arrays on $_POST. I'm currently using:
while(list($key_member,$member)=each($_POST['member_ids'])
&& list($key_amount,$amount)=each($_POST['payment_amounts']))
{
echo "MEMBER: $member<br>";
echo "AMOUNT: $amount<br><br>";
}
If I use one list() on either array it will print the info for that particular item. However, if I attempt to use multiple list() commands in the while, only the last list()ed item gets filled properly. Is list() doing some trickery in the background that's preventing it from working in a while loop?
Obviously the "easy" solution would be to use an index and simply force the issue, but I prefer enumerating -- and I'm honestly just curious as to
What am I doing wrong, and/or what is "broken" with list()?
bug? dunno.
here's a workaround.
while(list($key_member,$member)=each($_POST['member_ids'])){
list($key_amount,$amount)=each($_POST['payment_amounts']);
echo "MEMBER: $member<br>";
echo "AMOUNT: $amount<br><br>";
}
You could extract each array's keys using array_keys(), which produces an indexed array, then keep separate loop counters for each array:
$member_ids = array_keys($_POST['member_ids']);
$amounts = array_keys($_POST['payment_amounts']);
$mi = 0;
$am = 0;
while(1) {
...
$mi++
$am++;
if (count($member_ids) >= $mi) && (count(amounts) >= $am) {
break;
}
}
&& is evaluated in a short-circuit manner, the first statement to return false jumps out of it. In your case it stops to iterate as soon as the first array is at its end. list should work fine here, as it's a language construct which assigns variables.