I’m exporting some data from MySQL which can be selected through checkboxes on a page to a .txt file.
Now I have a function to “black out” middle digits of the numbers from MySQL, this function is working on my page, there it looks like this:
<?php
$sql="SELECT * FROM table";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<?php function blankit($word) {
return substr($word, 0,2). str_repeat("X",strlen($word)-4) . substr($word, strlen($word)-2,strlen($word));
}
?>
<?php while($rows=mysql_fetch_array($result, MYSQL_ASSOC)) {
$code = ($rows['used'] == '') ? blankit($rows['code']) : $rows['code']; ?>
<tr class="first">
<td class="tc"><font face="Arial, Helvetica, sans-serif"><? echo $rows['id']; ?></font></td>
<td class="tc"><font face="Arial, Helvetica, sans-serif"><? echo $code; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $rows['date']; ?></font></td>
<td class="tc"><font face="Arial, Helvetica, sans-serif"><? echo $rows['ip']; ?></font></td>
<td class="tc"><font face="Arial, Helvetica, sans-serif"><? echo $setusedpscde; ?></font></td>
<td class="tc"><input name="checkbox[]" type="checkbox" class="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
</tr>
<?php
}
?>
Now I want to apply this same function (blankit) when I export to the .txt file, my export code:
<?php
if ($_POST['exporttxt']) {
for($i=0;$i<count($_POST['checkbox']);$i++){
$export_id = $checkbox[$i];
$text = mysql_query("SELECT code FROM table WHERE id='$export_id'");
$text2 = mysql_fetch_assoc($text);
$text3 = $text2["code"];
$filename = "export";
$filedate = date("Y-m-d");
$fileext = ".txt";
$fileall = $filename.$filedate.$fileext;
ob_end_clean();
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment;filename=\"$fileall\"");
header("Content-Type: application/force-download");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: ".strlen($output).";\n");
echo chunk_split($text3, 16, "\n");
}
}
?>
I tried changing
$text3 = $text2["code"];
to
$text3 = ($text2['used'] == '') ? blankit($text2['code']) : $text2['code'];
But this X’ed out all the middle digits of every number, even if used was not =”
What do I have to change here ?
The problem is that for 1st query you selected all columns and in 2nd only the code column
In that case,
$text2['used'] == ''is always true as the value is null. You have to add ‘used’ column