This question already has answers here:
How to find the foreach index?
(14 answers)
Closed 7 years ago.
I am using this to create a mp3 list.
$mp3 = glob($directory . '*.mp3');
//print each file name
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one"><!--number goes here --></td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>
';
}
I want to add number with every item of the list like, first one: 1, second one:2 etc. how can I do it?
$listOfMp3 = glob($directory . '*.mp3');
foreach($listOfMp3 as $i => $mp3)
{
echo $i;
}
This is not the best solution, but you can use a counter. Here is an example:
$mp3 = glob($directory . '*.mp3');
$c = 0; //Set this value to 1 if you don't want to start with 0
foreach($mp3 as $mp3)
{
echo '
<tr>
<td class="one">'.$c.'</td>
<td class="one">'. str_replace('(BDalbum.com).mp3','',basename($mp3)) .'</td>
<td class="two">'. $_GET['s'] .'</td>
<td class="three">'. $_GET['a'] .'</td>
<td class="two">'. $_GET['a'] .'</td>
</tr>';
$c++;
}
So now $c will display the number of the file every time. Anyway, you can get the foreach key to do exactly the same.
Related
This question already has answers here:
Replace username part in email addresses into asterisks
(3 answers)
Partially hide email address in PHP
(19 answers)
How To Replace Some Characters With Asterisks
(5 answers)
Closed 2 years ago.
I have a table that show some data wallet address or email.
I need when is email then hide the letters before # like this ****#gmail.com
My table in the frontent php is:
<table class="table table-striped text-center"><thead><tr>
<th scope="col">Username</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
<?php
foreach ($withdrawHistory as $wd) {
echo '<tr><td>' . $wd["username"] . '</td>
<td>' . $wd["wallet"] . '</td>
</tr>'; }?> </tbody></table>
Is there a way to hide ?
<table class="table table-striped text-center"><thead><tr>
<th scope="col">Username</th>
<th scope="col">Address</th>
</tr>
</thead>
<tbody>
<?php
foreach ($withdrawHistory as $wd) {
echo '<tr><td>' . $wd["username"] . '</td>
<td>' . '****'.strstr($wd["wallet"], '#') . '</td>
</tr>'; }?> </tbody></table>
One way to do this is by using explode (inside the foreach loop) and then just echo $email.
$wallet = explode("#", $wd["wallet"]);
$email = "****#". $wallet[1];
echo $email;
If you want the number of letters before # match the number of * use this.
$wallet = explode("#", $wd["wallet"]);
$y = strlen($wallet[0]);
$hidden = "";
for ($x = 1; $x <= $y; $x++) {
$hidden .= "*";
}
$email = $hidden ."#". $wallet[1];
echo $email;
I have a question and I will try to explain the best way that I can
I have a table that show me names of accounts and each one of that accounts has it own information.
and I'm getting that information like this
On the page "verpersonagem.php" I have this: $idaccount = $_GET['accountId'];
but the url comes like this ../verpersonagem.php?account=
Why it's not getting the value?
Thanks in advance
you don't have yo use all this echo statement
try this
<?php
while ($row = mysqli_fetch_row($result))
{
echo '<tr>
<th> '. $row[0] .' </th>
<td>'. $row[2] .' </td>
<td>'. $row[1] .'</td>
<td>'. $row[3] .'</td>
<td>'. $row[4] .'</td>
</tr>';
}
?>
You should insert the href into the Table its TD instead of TH
Use a for loop instead and say count(rows)
Then $i < $rows and href=example.php?id=$i
Then use $_GET['id']
And do a database query which gets all the info about the id
for ($i=0; $i<=$lines; $i++)
{
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
This is my code, it read's from a text file and explode in table, but I have a little problem here cause this one needs to be link.
<td width="20%">'.$part[2].'</td>
.$part[2]. is just a word from file but it has query like www.somesite.com/?q= There at the end I need to have that
word from file
that kind of code did not work for me
<td width="20%"> <a herf='www.somesite.com/?q=''.$part[2].'> '.$part[2].' </a> </td>
I realy need some help with this...
<?php
//first, get the file...
$file = file('req.txt');
//now count the lines ..
$lines = count($file);
//start the table here..
echo'<table border="2" width="100%">';
echo'<tr>
<td width="20%">Naslov</td>
<td width="20%">Vrsta</td>
<td width="20%">IP</td>
<td width="20%">Dodano (DD.MM.YY - HH.MM)</td>
<td width="20%">Status</td>
</tr>';
//start the loop to get all lines in the table..
for ($i=0; $i<=$lines; $i++) {
//get each line and exlplode it..
$part = explode('|', $file[$i]);
//now start printing ..
echo'<tr>
<td width="20%">'.$part[0].'</td>
<td width="20%">'.$part[1].'</td>
<td width="20%">'.$part[2].'</td>
<td width="20%">'.$part[3].'</td>
<td width="20%">'.$part[4].'</td>
</tr>';
}
//close the table so HTML wont suffer :P
echo'</table>';
?>
This should produce this but ip column need to be link...
I think vprintf() is your friend.
<?php
$fmt = '<tr>
<td>%1$s</td>
<td>%2$s</td>
<td>%3$s</td>
<td>%4$s</td>
<td>%5%s</td>
</tr>';
for ($i=0; $i<=$lines; $i++)
{
// get each line and explode it..
$part = explode('|', $file[$i]);
// now start printing ..
vprintf($fmt, $part);
}
And put the width="20%" into your CSS.
I solve it alone with changing some values in input script "file writer"
$savestring = $title . "|" . $genre . "|<a href=http://www.example.com/ip?ip=" . $ip . ">" . $ip . "|" . $date . "|Za Naložit \n";
it works now ty anyway :)
If you believe this is a duplicate please let me now as I'm not totally sure what to search for to check, (I will remove if duplicate)
I have a list of of numbers in a mySql field called sess_times in a table called test_sess, the numbers for the first row are:
25, 38, 40, 50
is it possible to split these into php variable e.g:
$no1 = 25
$no2 = 38
$no3 = 40
$no4 = 50
I'm trying to put the individual numbers in a table, something like below:
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td> 1 </td>
<td><?php echo $no1; ?></td>
</tr>
<tr>
<td> 2 </td>
<td><?php echo $no2; ?></td>
</tr>
<tr>
<td> 3 </td>
<td><?php echo $no3; ?></td>
</tr>
<tr>
<td> 4 </td>
<td><?php echo $no4; ?></td>
</tr>
</table>
Any help with this would be great so thanks in advance for any answers.
HERE'S THE SOLUTION I USED, A COMBINATION OF 2 ANSWERS BELOW:
$data = '25,38,40,50';
$string = explode(",", $data);
$number = 0;
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($string); $i++)
{
$number = $number +1;
echo '
<tr>
<td>' . $number . '</td>
<td>' . $string[$i] . '</td>
</tr>
';
}
You can use PHP's explode function.
When you output everything, save it into a variable.
Then, $string = explode(",", $string); will produce this:
Array ( [0] => 28[1] => 38 [2] => 40 [3] => 50 )
To output this, you can then use $string[number] for each value.
You can explode them
$string="25,38,40,50";
$numbers=explode(",",$string);
print_r($numbers); // You can use them for printing like echo $numbers[0]; or 1 or 2 or 3
And if you don't want the result in an array and still want those 4 variable names you can do this
$string="25,38,40,50";
list($no1,$no2,$no3,$no4)=explode(",",$string);
This would give you the table output and it doesn't require the total amount to be known.
$data = '25,38,40,50';
$arr = explode(',', $data);
echo '<table cellspacing="0" cellpadding="0" border="0">';
for($i=0; $i<count($arr); $i++)
{
echo '
<tr>
<td>' . $i+1 . '</td>
<td>' . $arr[$i] . '</td>
</tr>
';
}
echo '</table>';
Use explode();
ex:
$numbers='25,38,40,50';
$numbers_array= explode(",", $no);
foreach($numbers_array as $key => $number)
{
$var_name='no'.(++$key);
$$var_name=$number;
}
echo $no1.'<br>;
echo $no2.'<br>;
echo $no3.'<br>;
echo $no4.'<br>;
<?php
$files = glob('users/*.xml');
foreach($files as $file) {
$xml = new SimpleXMLElement($file, 0, true);
echo '
<tr>
<td><input type="radio" name="browser" onclick="check(this.value)" value="'. basename($file) .'"></td>
<td class="alternate-row1">'. basename($file, '.xml') .'</td>
<td><span id="itm1" onclick="exchange(this.id)">'. $xml->name .'</span><input id="itm1b" class="replace" type="text" name="newname"></td>
<td class="alternate-row1">'. $xml->lastname .'</td>
<td>'. $xml->email .'</td>
<td class="alternate-row1">'. $xml->level .'</td>
<td>'. $xml->birthday .'</td>
<td class="alternate-row1">'. $xml->gender .'</td>
<td>'. $xml->age .'</td>
<td class="alternate-row1">'. $xml->country .'</td>
</tr>';
}
session_start();
if (isset($_POST['save'])){
$member = $_POST['newname'];
$dom = new DOMDocument();
$dom->load('users/' . basename($file) . '.xml');
$editname = $dom->getElementsByTagName('name');
$newname = $dom->createTextNode($member);
foreach ($editname as $edit) {
$edit->parentNode->replaceChild($newname, $edit);
}
$dom->save('users/' . basename($file) . '.xml');
}
?>
So I've made this code, as you can see it got DOM and Simple XML element.
I trying to replace child using $_POST throw input command, the input box is in the echo part above and I using js replacement code, it replace text to input text by onclick command (not really important for my question, it just for explaining).
Anyway I got little stuck with the code since it not working, I got set the $_POST, I try use some guides I found in Stack Overflow but it didn't work since it not matches my code.
Do you have any answer to me?
Thank you for the assistance, Y. D.
Example using simplexml ---> see live demo: http://codepad.viper-7.com/2oR5Wk
$xmlstr = <<<XML
<root>
<head>
<name id="1">test</name>
<name id="2">file</name>
<name id="3">name</name>
</head>
</root>
XML;
$xml = simplexml_load_string($xmlstr);
echo "<pre>";
var_dump($xml);
echo "</pre>";
$x = count($xml->head->name)-1;
for ($x; $x>=0; $x--) {
$xml->head->name[$x] = "newname";
}
echo "<pre>";
var_dump($xml);
echo "</pre>";