I'm trying to make Drupal print a list with two different fields in the same array. So first comes field A, then field B and it prints in this way until the whole array is printed.
The result I'm trying to get is something like
<tr>
<td>Field_1[value1]</td>
<td>Field_2[value1]</td>
</tr><tr>
<td>Field_1[**value'n'**]</td>
<td>Field_2[**value'n'**]</td>
</tr>
Until all values are printed.
EDIT.
Figured out one way of achieving this directly in node--testnode.tpl.php.
<table>
<?php if ($content['field_test'][1]): ?>
<tr><td><?php print render($content['field_test'][0])?></td><td><?php print render($content['field_test'][1])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][3]): ?>
<tr><td><?php print render($content['field_test'][2])?></td><td><?php print render($content['field_test'][3])?></td></tr>
<?php endif; ?>
<?php if ($content['field_test'][5]): ?>
<tr><td><?php print render($content['field_test'][4])?></td><td><?php print render($content['field_test'][5])?></td></tr>
<?php endif; ?>
</table>
Second fix, only manual work is to say how many repetitions you want.
<dl class="My Class">
<?php
$i = 0;
$counter = 2 * render ($content['field_counter_slides'][0]) -1;
while ($i <= $counter):
if ($content['field_test'][1]){
echo "<dt>";
print render ($content['field_test'][$i]);
$i++;
echo "</dt><dd>";
print render ($content['field_test'][$i]);
echo "</dd>";
$i++;
}
endwhile;
?>
</dl>
It's quite hard to answer without knowing more but something like this would probably do it:
$header = array();
$rows = array();
foreach ($node->field_my_field[$langcode] as $key => $field_entry) {
$rows[] = array(
$field_entry['value'],
$node->field_my_second_field[$langcode][$key]['value']
);
}
$output = theme('table', array('header' => $header, 'rows' => $rows));
You'd have to make sure yourself that there are equal numbers of entries for each field so that there will always be a field_my_second_field entry for each run through the foreach loop.
Related
data is from my database they are not same value of data. and my html display are not good to view, is there any way to create empty td?
<?php while($team= mysqli_fetch_assoc($team_set)){ ?>
<td><?php echo htmlentities($team['tname']); ?></td>
<?php while($grade = mysqli_fetch_assoc($grade_set)){ ?>
<td><?php echo htmlentities($grade['points']); ?></td>
<?php } mysqli_free_result($grade_set); ?>
<td><?php echo htmlentities($team['sum']); ?></td>
<?php } mysqli_free_result($team_set); ?>
I would reorganize the structure of the middle part, i.e. this part of your code:
<?php while($grade = mysqli_fetch_assoc($grade_set)){ ?>
<td><?php echo htmlentities($grade['points']); ?></td>
<?php } mysqli_free_result($grade_set); ?>
Here every entry is put into its own table cell, creating what you describe in your question (different amount of cells in each row). I would instead put all these entries into one cell:
<td class="points_cell">
<?php while($grade = mysqli_fetch_assoc($grade_set)){ ?>
<span class="points"><?php echo htmlentities($grade['points']); ?></span>
<?php } mysqli_free_result($grade_set); ?>
</td>
As you can see, I wrapped the single entries into span tags with a class (.points) and created and applied the class .points_cell to the cell that wraps them. For these two classes you can create CSS rules similar to this:
.points_cell {
white-space: nowrap;
}
.points {
display: inline-block;
padding: 0 10px;
}
white-space: nowrap; on .points_cell makes sure the .points spans are not put below each other, but in one line. The side padding in .points creates sufficient distance between the entries - this value can of course be adjusted as needed.
You could also create CSS borders (left and right) or alternating background colors for those span elements if you want to set them apart in a more obvious way.
To output table cells using only PHP, you'd need to pre-loop your arrays, find the max in any row, then loop again that many times when you actually display.
Here I'm ls using a temporary array to hold the results so you don't need to call the database twice. I made some assumptions about your SQL result set, so you may need to modify it slightly.
<?php
// Call database, pre-looping result set
$max = 1;
$table_arr = array();
while( $team = mysqli_fetch_assoc($team_set) ){
$tmp = array();
while( $grade = mysqli_fetch_assoc($grade_set) ){
$tmp[] = $grade['points'];
}
mysqli_free_result($grade_set);
// Store results for later use
$table_arr[] = array(
'tname' => $team['tname'],
'sum' => $team['sum '],
'points' => $tmp,
);
// Get max cells in any result set
$max = max( $max, count($tmp) );
}
mysqli_free_result($team_set);
?>
<?php
// Loop $table_array to actually output things
foreach ($table_array as $team) {
?>
<td><?php echo htmlentities($team['tname']); ?></td>
<?php
// loop $max times, output points if present else empty cell
for ($x = 0; $x < $max; $x++) {
$points = isset( $team['points'][ $x ] ) ? $team['points'][ $x ] : ' ';
?>
<td><?php echo htmlentities($points); ?></td>
<?php } ?>
<td><?php echo htmlentities($team['sum']); ?></td>
<?php } ?>
I have an HTML search form:
<form method="post">
<input type="text" name="term" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
I then have a foreach loop to check if the array item contains the search term:
<?php if (is_array($data['page'])): ?>
<?php foreach ($data['page'] as $item): ?>
<?php foreach($item as $item_member): ?>
<?php if(strpos($item_member, $_POST['term']) !== FALSE): ?>
<tr>
<?php foreach ($columns as $key => $column): ?>
<?php $key = parse_class($key); ?>
<td class="<?php echo $key['class']; ?>"><a class="cell-link"
href="<?php echo $details_link . $item[$details_id_field]; ?>">
<?php echo (!empty($item[$key['column']])) ? $item[$key['column']] : ' '; ?>
</a></td>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<?php endforeach; ?>
<?php endforeach; ?>
<?php endif; ?>
Here is a more readable version
if(is_array($data['page'])){
foreach($data['page'] as $item){
if(array_search($_POST['term'], $item)){
echo "<tr>";
foreach($columns as $key => $column){
$key = parse_class($key);
echo '<td class="' . $key['class'] . '"><a class="cell-link" href="' . $details_link . $item[$details_id_field] . '">';
echo (!empty($item[$key['column']])) ? $item[$key['column']] : ' ';
echo '</a></td>';
}
echo '</tr>';
}
}
}
At the moment, the search only works if the search term is exactly the same as the name in the item.
The array item contains the following information:
234|test|21|Jul 28, 2013|0%|1375026328|1375026328|Test)/
I'm really only interested in matching the second value in item (in the example above "test" with the search term "term"
I have tried in_array, strpos with no luck.
For the example above, at the moment a result is only brought up if I enter the exact text "test" in the search form. If I enter "tes" for example which is an incomplete search phrase, nothing shows.
If I understand correctly $item holds the array you referred to.
Add another foreach to iterate the elements of $item and for every one of the items preform a strpos($item_member, $_POST['term']) check instead of array_search:
<?php foreach($item as $item_member)): ?>
<?php if(strpos($item_member, $_POST['term']) !== FALSE): ?>
<?php // here do what you gotta do ?>
<?php endif; ?>
<?php endforeach; ?>
BUT, if you already know that you're looking to match only the second element - you can skip this foreach and simply do:
<?php if(strpos($item[1], $_POST['term']) !== FALSE): ?>
This check will return true (or actually the beginning index of the match that was found) for any valid sub-string of test - so if you want only sub-string that start at index 0 you can change the test instead of: !== FALSE
to
=== 0
By the way - the : syntax is hard to read - I would change it.
First of all, I don't know what you intend 234|test|21|Jul 28, 2013|0%|1375026328|1375026328|Test)/ to mean. Is that a string at the 0 index of the array? Are the pipes just a way to visually separate the array elements?
Also, your code is also very hard to read, so I didn't read it. That said, I think I understand what you're trying to do. Let's say you have an array with some stuff in it:
$arr = array(
234,
'test',
'Jul 28, 2013',
'testers',
);
And let's say you have a user input string:
$input = 'tes';
We want to determine if any of the elements in the array contain this string:
$matches = array();
foreach($arr as $element) {
if (strpos("$element", $input) !== false) {
#match found, store it
$matches[] = $element;
}
}
var_dump($matches); //contains two elements, 'test', and 'testers'
Output of that var_dump:
array(2) {
[0]=>
string(4) "test"
[1]=>
string(7) "testers"
}
You should be able to adapt this to your specific use case. By the way, the quotes around $element in that strpos call are to cast it to a string. I doubt this is necessary in PHP (can't recall), but just in case ...
I have obtained an array output from a curl command. Now, I need to place that output in a table. I have written the entire code in php.
The .phtml part is:
echo "<table border='1'>
<tr><td>NAME</td></tr>
<tr><td>COUNTRY</td></tr>
</table>";
The loop part is:
foreach($arr['fruits'] as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->NAME["Name"];}?></td></tr>
<tr><td><?php echo $fruit->COUNTRY["Country"];}?></td></tr>
I'm very very new to all this. So, I don't really know what I'm exactly doing. Thanks in advance...
Use it like this :
Considering your array structure is like this :
$your_array = array(array("name"=>"prasanth","country"=>"India"),
array("name"=>"bendra","country"=>"India"),
array("name"=>"User","country"=>"US")
);
<?php foreach($your_array as $key=>$fruit) { ?>
<tr><td><?php echo $fruit['name'];?></td></tr>
<tr><td><?php echo $fruit['country'];?></td></tr>
<?php } ?>
Here's all code.
<?php
echo "<table border='1'>
<tr><td>NAME</td></tr>
<tr><td>COUNTRY</td></tr>";
foreach($arr['fruits'] as $key=>$fruit) {
?>
<tr><td><?php echo $fruit->NAME["Name"];?></td></tr>
<tr><td><?php echo $fruit->COUNTRY["Country"];?></td></tr>
<?php ;}
echo "</table>";
?>
Your problem is with this sort of thing:
$fruit->NAME["Name"]
This means in the $fruit object look for a property called NAME that is an array containing the key "Name".
Also, your table isn't very tabular with only one cell per row. I would try something like this (if $fruit is really an array not an object):
<table border="1">
<tr><th>Name</th><th>Country</th></tr>
<?php
foreach( $arr['fruits'] as $fruit ) {
echo "<tr><td>$fruit['NAME']</td><td>$fruit['COUNTRY']</td></tr>";
}
?>
</table>
We can't help you without knowing the actual structure of $arr['fruits']
I was trying to do simple news system with CodeIgniter, and I've done that. But I've problem with displaying the results. Actually, they're displaying right as I wanted to, but I can not display more than one of them.
I'm using 'foreach' for that, and the thing is, that, when I'm putting my style inside foreach, and displaying it in it, then it works well. But when I'm trying to define these datas first, and then display them (outside the foreach), then only one record is showing up.
What I wanted to do, and doesn't work:
<?php foreach ($posts as $row) {
$title = $row['title'];
$text = $row['text'];
$date = $row['date'];
$autor_id = $row['user_id'];
}
echo $title; ?>
What works, but isn't comfortable to use for me:
<?php foreach ($posts as $p): ?>
<h2><?php echo htmlspecialchars($p['title']); ?></h2>
<p><?php echo nl2br(htmlspecialchars($p['text'])); ?></p>
<?php endforeach; ?>
Is it even possible to do this that way that I'm trying to do?
Just combine the two:
<?php foreach ($posts as $row) {
$title = $row['title'];
$text = $row['text'];
$date = $row['date'];
$autor_id = $row['user_id'];?>
<h2><?php echo htmlspecialchars($title); ?></h2>
<p><?php echo nl2br(htmlspecialchars($text)); ?></p>
<?php } ?>
Or use echo inside the loop and you don't need to break the PHP tags.
I have a simple working PHP script to write an HTML table from a text file that contains a single column of data that contains HTML links. This writes the data horizontally in a row across 5 or 6 columns as I want it to. But I am looking to set up a script with a loop that will take this list of data and input it into the table until it finishes the data list, so that I do not have to hard code each table cell individually. Just let the script create each table cell, at 5 or 6 columns across (whichever I need for this specific table), go to the next row, etc., until it runs out of data. The data in the data file will be added to on a regular basis, so the table will not be of a certain fixed length forever. I am using the echo command so that I can add some more HTML formatting later on.
Even though my existing script is simple and it works, if you can think of a better way of doing what I am trying to do, all suggestions are appreciated.
Thanks in advance, Stan...
PHP code follows:
<?php
$item = #fopen('linklist.txt', "r");
if ($item) { while (!feof($item)) { $lines[] = fgets($item, 4096); } fclose($item); }
echo'
<TABLE border="1">
<TR>
<TD>'.($lines[1]).'</td>
<TD>'.($lines[2]).'</td>
<TD>'.($lines[3]).'</td>
<TD>'.($lines[4]).'</td>
<TD>'.($lines[5]).'</td>
<TD>'.($lines[6]).'</td>
</tr>
<TR>
<TD>'.($lines[7]).'</td>
<TD>'.($lines[8]).'</td>
<TD>'.($lines[9]).'</td>
<TD>'.($lines[10]).'</td>
<TD>'.($lines[11]).'</td>
<TD>'.($lines[12]).'</td>
</tr>
<!-- And So On, And So On, ETC -->
</table>'
?>
<?php
echo '<table border="1"><tr>';
for($i=0; $i<sizeof($lines); $i++) {
echo '<td>'.$lines[$i].'</td>';
if(($i+1)%6==0 && $i!=sizeof($lines)-1) echo '</tr><tr>';
}
echo '</tr></table>';
?>
Explanation:
Repeats through each "line" and writes the <td>value</td>
If a line is a multiple of 6, after writing the value, then close the row, and open another (unless it's the last one, since it will close the row after the loop as well.
(I assume you meant to start on $line[0], but if you really meant to start on $line[1], just change the $i=0; to $i=1;, remove the +1 in the row check, and change $i<sizeof to $i<=sizeof
$lines = chunk_split($lines,6);
?>
<TABLE border="1">
<? foreach ($lines as $row): ?>
<TR>
<? foreach ($row as $value): ?>
<TD><?=$value?></td>
<? endforeach ?>
<TR>
<? endforeach ?>
</TABLE>
In pseudo code...
x = 0
echo '<TABLE border="1">'
For each $line in $lines {
x = x + 1
if x = 1 {
echo '<TR>'
}
echo <TD>'.($line).'</TD>
if x = 6 {
echo '</TR>'
x = 0
}
}
echo '</TABLE>'
Note the use of the $line object to hold the value of the $line array element.
hth
If you use a loop, you can also avoid reading the complete file into memory, which might be beneficial:
<?php
$item = #fopen('linklist.txt', "r");
if ($item) {
echo'<TABLE border="1">';
$i=0;
$lines=array();
while (!feof($item)) {
$line[] = fgets($item, 4096);
$i++;
if ($i==6) {
echo "<tr>";
echo '<TD>'.($lines[0]).'</td>';
echo '<TD>'.($lines[1]).'</td>';
echo '<TD>'.($lines[2]).'</td>';
echo '<TD>'.($lines[3]).'</td>';
echo '<TD>'.($lines[4]).'</td>';
echo '<TD>'.($lines[5]).'</td>';
echo "<tr>";
$i=0;
$lines=array();
}
}
echo '</table>';
fclose($item);
}
?>