Undefined offset: 2 ( php function ) [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
So-.. I was looking up how to fix this function; but I can't seem to find out. Can anyone give a hand?
I get an error on this line ( I'll include the php function ).
PHP function:
while($sql_row = mysqli_fetch_row($result))
Actual faulty line:
<tr>
<td><a <?php echo 'href="character_staff.php?action=manage&name=' . $sql_row[0] . '"'; ?>><?php echo $sql_row[0]; ?></a></td>
<td><a <?php echo 'href="character_staff.php?action=manage&name=' . $sql_row[0] . '"'; ?>><?php echo $sql_row[2]; ?></a></td>
</tr>
I can't seem to find out how to fix it. It has to change the URL into a link that contains text ( IE: character_staff.php?action=manage&name=account_name )

The problem is here:
<?php echo $sql_row[2]; ?>
Your code is attempting to echo the third element of the $sql_row array, but the array does not contain three elements. There are two ways to fix the problem: change the code to reference only elements that exist in the array, or change the array to contain more elements.
Note: in case you are wondering why $sql_row[2] looks for the third element, it is because arrays are zero-indexed.
$sql_row[0]
returns the first element.
$sql_row[1]
returns the second element, and so on.

Related

how to Insert comma between values in php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
My value is coming like this with two enters. please see below :
125
124
132
I am getting this value by php variable and want to get values with commas in new php variable.
i want like this 125,124,132
anyone have an idea for that please?
$str = "125
124
132";
$str = str_replace("\r\n\r\n", ",", $str);
echo $str;
Try the above code. Please let know if this worked.
You can directly add commas between numbers using
number_format() function of PHP
<?php
echo number_format("125124132")."<br>";
?>
Answer:- 125,124,132
Try the above given code and let me know if this worked.....

Merge <tr> <td> in php foreach [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have a dynamic table where a foreach loop generate every tr and the first td values
My problem is that every tr has 2nd 3rd 4th and 5th td column. These are also generated by a new foreach.
What is the smartest way to synchronise these loops to show data correctly?
I think this is very simple:
<?php
$rows = '5'; // Number of rows that you want
echo "<table>";
for($tr=1;$tr<=$rows;$tr++){
echo "<tr>";
echo "<td>{$tr}*1</td>";
for($td=2;$td<=5;$td++){
echo "<td>{$tr}*{$td}</td>";
}
echo "</tr>";
}
echo "</table>";
?>

add space in html when using php echo [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am writing a html class attribute which need space to allow multiple values. However, among these values, there are variables and constants. Code shows below:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="value1 value2">
Here I want value1 in the class to be a variable like $itemTitle, but still to maintain the "value2" with a space between them.
I have tried many method like add &nbsp and using
echo $itemName." "."value2";
but seems didn't work.
Any ideas?
Thanks!
Solved:
Solution1:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$item->category_id"." "."value0"; ?>" >
Solution2:
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$item->category_id Value0"; ?>" >
Thanks!
<?php
echo "$itemName value2";
?>
If you use double quotes, you can drop the variable right in there, no concatenation needed!
<article id="portfolio-item-1" data-loader="include/ajax/portfolio-ajax-image.php" class="<?php echo "$itemName value2"; ?>">
It is not an error to have a space without a class on each side.
echo '<article... class="'.$itemName.' value2"...
$id = 'portfolio-item-1';
$url = 'include/ajax/portfolio-ajax-image.php';
echo '<article id="'.$id.'" data-loader="'.$url.'" class="'.$itemName.' value2">';
will work, for instance.

find and replace special characters in PHP [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Please tell me how to do a find and replace in PHP.
I nee to find Entry="OK" and replace with OK in a xml file.
I tried str_replace but confused because of the = and " in it.
Even though I'm not sure of the environment you're working in, here's a php sample that I think does what you want:
<html>
<?php
$teststr = "Entry=\"OK\"";
print $teststr . "<br>";
$newanswer = str_replace($teststr, "Entry=OK", $teststr);
print $newanswer . "<br>";
?>
</html>
Try this way
str_replace('Entry="OK"','OK',$var);
You can use str_replace its working
$r = 'Entry="OK" tyyuu hhh';
echo str_replace($r,'Entry="OK"','OK');
if any error found then u use preg_replace with corresponding patteren

PHP: easy random picture code [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My code is random but its does not print out the picture just the text. When I add the IMAGE tag. Its go all wrong. Where do I go wrong?
$plaatje[] = 'afbeelding1.jpg';
$plaatje[] = 'afbeelding2.jpg';
$plaatje[] = 'afbeelding3.jpg';
$plaatje[] = 'afbeelding4.jpg';
$nummer2 = mt_rand(1,4);
echo "$plaatje[$nummer2]";
Use array_rand() - Picks one or more random entries out of an array
echo $plaatje[array_rand($plaatje)];
With an image tag, something like:
<img src="<?php echo $plaatje[array_rand($plaatje)]; ?>" />
or
echo "<img src='".$plaatje[array_rand($plaatje)]."' />";
Here is the documentation
Although you should add the image tag to show where it goes wrong, your code already has a problem as array indices are 0-based.
So you would need:
$nummer2 = mt_rand(0,3);

Categories